一.编写脚本
#!/bin/bash
TomcatID=$(netstat -nlp | awk -F'[ /]*' '/8080/{print $(7)}')
StartTomcat=/data/server/tomcat/bin/startup.sh
TomcatCache=/data/server/tomcat/work
WebUrl=http://xxxxx
GetPageInfo=/data/shfile/logs/Monitor_business_tomcat.Info
TomcatMonitorLog=/data/shfile/logs/Monitor_business_tomcat.log
Monitor()
{
echo "[info]开始监控 [tomcat]...[$(date +'%F %H:%M:%S')]"
if [[ $TomcatID ]];then # 这里判断TOMCAT进程是否存在
echo "[info]当前tomcat进程ID为:$TomcatID,继续检测页面..."
# 检测是否启动成功(成功的话页面会返回状态"200")
TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code})
if [ $TomcatServiceCode -eq 200 ];then
echo "[info]页面返回码为$TomcatServiceCode,tomcat启动成功,测试页面正常......"
else
echo "[error]tomcat页面出错,请注意......状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo"
echo "[error]页面访问出错,开始重启tomcat"
kill -9 $TomcatID # 杀掉原tomcat进程
sleep 3
$StartTomcat
fi
else
echo "[error]tomcat进程不存在!tomcat开始自动重启..."
echo "[info]$StartTomcat,请稍候......"
$StartTomcat
fi
echo "------------------------------"
}
Monitor>>$TomcatMonitorLog
如果有关于JAVA_HOME的报错可以在前面加一句export JAVA_HOME=/usr/java/jdk
二.设置定时业务
vim /etc/crontab
*/1 * * * * root bash /data/shfile/monitor.sh
需要给脚本加上对应用户的执行权限,日志文件夹加上可写权限
网友评论