码迷,mamicode.com
首页 > 其他好文 > 详细

bash脚本之软件看门狗示例

时间:2015-04-08 21:14:52      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

#!/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

bash脚本之软件看门狗示例

标签:

原文地址:http://www.cnblogs.com/jayzee/p/4403597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!