您的位置:首页 > 运维架构 > Shell

通过shell脚本监控tomcat运行情况

2018-02-05 17:25 1071 查看

记录关于通过shell脚本监控tomcat运行情况

近期项目需要,磕磕绊绊写了几个shell脚本监控自己的程序是否正常运行,首先在应用代码中增加固定格式的日志,并记录当前时间,可以使shell脚本获取到日志中最新一条的日志记录,获取到时间并跟系统当前时间对比,如超过设置的阈值,如5分钟,则执行重启或发送预警邮件。

1.通过脚本重启tomcat

#!/bin/sh
cd /appl/datarcv/importDB/apache-tomcat-7.0.61/bin
sh shutdown.sh
if [ $? -eq 0 ];then
pid=$(ps -ef | grep apache-tomcat | grep -v grep | awk '{print $2}')
fi
if [ -n "$pid" ];then
echo "tomcat process id: $pid"
kill -9 ${pid}
else
echo "tomcat process not exist,begin to start"
sh startup.sh
exit;
fi
if [ $? -eq 0 ];then
echo "tomcat stop success,bengin to start"
sh startup.sh
else
echo "tomcat stop fail"
fi


2.通过shell脚本监控tomcat固定日志的输出,达到tomcat应用monitor的效果

cd /appl/datarcv/importDB/log
#get time from info.log
logtime=$(grep -n '_run_ok' info.log | tail -n 1|awk '{print $9}')
echo "logTime:"${logtime}
nowTime=`date +%s`
echo "nowTieme:"${nowTime}
interval=`expr $nowTime - $logtime`
echo $interval
if [ $interval -gt 300 ];then
echo "restart tomcat"
cd /appl/datarcv/bin
sh restartTomcat.sh
mailx -s "Cn002 tomcat error,will restart,please check again."  youxiang@qq.com < monitor.log

fi-------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息