#!/bin/sh# file timestamp to record the heartbeat of spring-rest-Servertimestamp=/usr/apache-tomcat-7.0. the/logs/timestamp.log# Timeout 60stimeout=60000# error Logstderr=/usr/apache-tomcat-7.0. the/logs/tomcat-watchdog.log#1means need to Restartrestart=0# the error infoerrinfo="'# Check whether file existsif[ -e $timestamp]; then# Check whether contains valueif[ -s $timestamp]; Thenstart_time=$ (cat $timestamp) # integer Checkif[1-eq ' echo $start _time|grep-p"^-?\d+$"|WC-l ']; Thenend_time=$ (Expr $ (date +'%s') \* +) Sub=$ (expr $end _time-$start _time-$timeout) #ifThe Sub value isLarger than0, means timeoutif[0-Lt $sub]; Thenerrinfo= $timestamp'timestamp timeout.'Restart=1fiElseErrinfo= $timestamp'contains Non-integer.'Restart=1fiElseErrinfo= $timestamp'doesnt contain value.'Restart=1fiElseErrinfo= $timestamp'doesnt exist.'Restart=1fi# kill Tomcat and restartif[1-eq $restart]; then echo $ (date+'%y-%m-%d%h:%m:%s')'kill and restart Tomcat, cause by:'$errinfo >>$stderr # Kill the Tomcat Processpid=$ (PS ax|grep tomcat|grep-v"grep"|awk'{print $}') echo $pid| whileRead Line Doif[""!="$line" ] ; then$ (Kill-9$line) fidone# start the Tomcatservice tomcat Startfiexit
Some notes
1./usr/apache-tomcat-7.0.42/logs/ Timestamp.log is a timestamp file for the Spring-rest-server program under the Tomcat container, and if the Spring-rest-server program runs properly, it will constantly write a timestamp to the file
2. The principle of the above watchdog script is to constantly detect whether the time of the file mentioned above is timed out, and if so, restart Tomcat
Software watchdog example of bash script