Software watchdog example of bash script

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.