之前在网上查的脚本运行总是无法正常监测,后面改了一些东西后才正常,现在记录希望对大家有所帮助。
#!/bin/sh source /etc/profile TomcatID=$(ps -ef |grep zhkh_dwn |grep -w 'zhkh_dwn'|grep -v 'grep'|awk '{print $2}') StartTomcat=/home/tomcat/bin/startup.sh EndTomcat=/home/tomcat/bin/shutdown.sh TomcatCache=/home/tomcat/work WebUrl=http://172.16.20.112:8082/mypreject/loginController.do?login GetPageInfo=/root/TomcatMonitor_DW.Info TomcatMonitorLog=/root/TomcatMonitor_DW.log Monitor() { echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]" if [ $TomcatID ];then #这里判断Tomcat进程是否存在 echo "[info]当前tomcat进程ID为:$TomcatID,继续检测页面..." TomcatServiceCode=$(curl -s -o $GetPageInfo --connect-timeout 10 $WebUrl -w %{http_code} --noproxy "*") if [ $TomcatServiceCode == 200 ];then echo "[info]页面返回码为:$TomcatServiceCode,tomcat启动成功,测试页面正常" else echo "[error]tomcat页面出错,状态码为:$TomcatServiceCode,错误日志已输出到:$GetPageInfo" echo "[error] 页面访问出错,开始重启tomcat" kill -9 $TomcatID # 杀掉原tomcat进程 sleep 3 rm -rf $TomcatCache # 清理tomcat缓存 $StartTomcat fi else echo "[error]tomcat进程不存在!tomcat开始自动重启..." echo "[info]$StartTomcat,请稍候... " rm -rf $TomcatCache $StartTomcat fi echo "--------------------------" } Monitor>>$TomcatMonitorLog
修改位置:1.加上环境加载:source /etc/profile
2.if [ $TomcatServiceCode == 200 ];then 中 '-eq' 改成了'=='