tomcat监控脚本
1 #!/bin/sh 2 3 # func:自动监控tomcat脚本并且执行重启操作 4 5 6 # 获取tomcat进程ID(其中[grep -w 'tomcat']代码中的tomcat需要替换为你的tomcat文件夹名) 7 TomcatID=$(ps -ef |grep apache-tomcat-7.0.54/conf |grep -w 'tomcat'|grep -v 'grep'|awk '{print $2}') 8 9 # tomcat启动程序(这里注意tomcat实际安装的路径) 10 StartTomcat=/usr/local/apache-tomcat-7.0.54/bin/startup.sh 11 TomcatCache=/usr/local/apache-tomcat-7.0.54/work 12 13 #定义要监控的页面地址 14 WebUrl=http://localhost:8081 15 16 #日志输出 17 GetPageInfo=/tmp/TomcatMonitor.Info 18 TomcatMonitorLog=/tmp/TomcatMonitor.log 19 20 Monitor() 21 { 22 echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]" 23 if [ $TomcatID ];then #这里判断Tomcat进程是否存在 24 echo "[info]当前tomcat进程ID为:$TomcatID,继续检测页面..." 25 # 检测是否启动成功(成功的话页面会返回状态"200") 26 TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code}) 27 if [ $TomcatServiceCode -eq 200 ];then 28 echo "[info]页面返回码为$TomcatServiceCode,tomcat启动成功,测试页面正常" 29 else 30 echo "[error]tomcat页面出错,请注意...状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo" 31 echo "[error]页面访问出错,开始重启tomcat" 32 rm -rf $TomcatCache # 清理tomcat缓存 33 echo "[error]$TomcatCache 清理tomcat缓存" 34 kill -9 $TomcatID # 杀掉原tomcat进程 35 echo "[error]$TomcatID 杀掉原tomcat进程" 36 sleep 3 37 $StartTomcat 38 fi 39 else 40 echo "[error]tomcat进程不存在!tomcat开始自动重启..." 41 echo "[info]$StartTomcat,请稍候..." 42 rm -rf $TomcatCache 43 $StartTomcat 44 fi 45 echo "--------------------------" 46 } 47 Monitor>>$TomcatMonitorLog 48 echo -e "#\t" >> /test.log
监控tomcat进程,如果服务宕了,将服务重启,相关日志输出到tmp下。