#!/bin/sh
#自动监控tomcat脚本并且执行重启操作#获取tomcat_IDTomcatID=`ps -ef|grep tomcat|grep -v "grep"|grep -v "monitor_tomcat"|awk '{print $2}'`Tomcatbag=`ls /usr/local/apache-tomcat-7.0.42/webapps -t |head -1`#tomcat启动程序 StartTomcat=/home/deployer/scripts/monitor_restart.sh TomcatCache=/usr/local/apache-tomcat-7.0.42/work/# 定义要监控的页面地址 #积分 WebUrl=http://127.0.0.1:8080/integral/health/check WebUrl=http://127.0.0.1:8080/$Tomcatbag/healthCheck/verify# 日志输出 GetPageInfo=/home/deployer/scripts/monitor_logs/error_$(date +'%F').log TomcatMonitorLog=/home/deployer/scripts/monitor_logs/TomcatMonitor_$(date +'%F').logMonitor() { echo "[info]开始监控tomcat...[$(date +'%F %H:%M:%S')]" if [[ $TomcatID ]];then echo "[info]当前tomcat进程ID为:$TomcatID,检测页面..." TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 30 $WebUrl -w %{http_code}) echo $TomcatServiceCode if [ $TomcatServiceCode -eq 200 ];then echo "[info]页面返回码为$TomcatServiceCode,tomcat启动成功,测试页面正常......" else echo "[error]tomcat页面出错,请注意......状态码为$TomcatServiceCode,错误日志已输出到$GetPageInfo" echo "[error]页面访问出错,开始重启tomcat" kill -9 $TomcatID sleep 3 rm -rf $TomcatCache $StartTomcat fi else echo "[error]tomcat进程不存在!tomcat开始自动重启..." echo "[info]$StartTomcat,请稍候......" rm -rf $TomcatCache $StartTomcat fi echo "------------------------------" }Monitor>>$TomcatMonitorLogcd /home/deployer/scripts/monitor_logsfind . -maxdepth 2 -mtime +5 -name "*.log" -exec rm -rf {} \;