该脚本用于监控tomcat服务器是否可用,如果服务不可用则重启tomcat
略微修改后也可以用于其他服务的监控
monitor.sh 脚本如下
1 #!/bin/sh 2 3 4 # 定义要监控的页面地址 5 WebUrl="http://xxxxxxx.com" 6 GetPageInfo=/dev/null 7 8 9 while [ "1" = "1" ] 10 do 11 12 TomcatServiceCode=$(curl -s -o $GetPageInfo -m 10 --connect-timeout 10 $WebUrl -w %{http_code}) 13 echo "`date "+%Y-%m-%d %H:%M:%S"` : TomcatServiceCode:"$TomcatServiceCode 14 15 #如果返回不是http 200 ,说明tomcat已经挂断 16 #需要杀掉进程,并重启tomcat 17 if [ $TomcatServiceCode -eq 200 ];then 18 echo "`date "+%Y-%m-%d %H:%M:%S"` :tomcat is okay." 19 else 20 ID=`ps -ef | grep tomcat | grep -v "grep" | awk '{print $2}'` 21 echo $ID 22 for id in $ID 23 do 24 kill -9 $id 25 echo "`date "+%Y-%m-%d %H:%M:%S"` : killed $id" 26 done 27 echo "`date "+%Y-%m-%d %H:%M:%S"` : PID have been killed" 28 echo "`date "+%Y-%m-%d %H:%M:%S"` : begin to restart the tomcat." 29 service tomcat start 30 echo "`date "+%Y-%m-%d %H:%M:%S"` : tomcat have been restarted." 31 fi 32 echo "check done." 33 echo "`date "+%Y-%m-%d %H:%M:%S"` sleep 60s and wait next check.." 34 sleep 60s 35 done
启动脚本
nohup ./monitor.sh >/dev/null 2>&1