标签:
#!/bin/sh # file timestamp to record the heartbeat of spring-rest-server timestamp=/usr/apache-tomcat-7.0.42/logs/timestamp.log # timeout 60s timeout=60000 # error log stderr=/usr/apache-tomcat-7.0.42/logs/tomcat-watchdog.log # 1 means need to restart restart=0 # the error info errinfo=‘‘ # check whether file exists if [ -e $timestamp ] ; then # check whether contains value if [ -s $timestamp ] ; then start_time=$(cat $timestamp) # integer check if [ 1 -eq `echo $start_time|grep -P "^-?\d+$"|wc -l` ] ; then end_time=$(expr $(date +‘%s‘) \* 1000) sub=$(expr $end_time - $start_time - $timeout) # if the sub value is larger than 0, means timeout if [ 0 -lt $sub ] ; then errinfo=$timestamp‘ timestamp timeout.‘ restart=1 fi else errinfo=$timestamp‘ contains non-integer.‘ restart=1 fi else errinfo=$timestamp‘ doesnt contain value.‘ restart=1 fi else errinfo=$timestamp‘ doesnt exist.‘ restart=1 fi # kill tomcat and restart if [ 1 -eq $restart ] ; then echo $(date +‘%Y-%m-%d %H:%M:%S‘)‘ kill and restart tomcat, cause by : ‘$errinfo >> $stderr # kill the tomcat process pid=$(ps ax|grep tomcat|grep -v "grep"|awk ‘{print $1}‘) echo $pid|while read line do if [ "" != "$line" ] ; then $(kill -9 $line) fi done # start the tomcat service tomcat start fi exit
1. /usr/apache-tomcat-7.0.42/logs/timestamp.log为tomcat容器下的spring-rest-server程序的时间戳文件,如果spring-rest-server程序正常运行,它将会不断的写一个时间戳到该文件
2. 上述看门狗脚本的原理即不断的去检测上面提到的文件的时间是否超时,是的话重启tomcat
标签:
原文地址:http://www.cnblogs.com/jayzee/p/4403597.html