A simple shell script that monitors / supervises my Tomcat process and restarts it if it can not be reached.
Note:
- It does not access the proc-filesystem of Linux. This would be another feasible approach.
- The script is to be run on the same machine as the start-script for Tomcat.
- And finally: It's suitable to supervise a webserver as well, it doesn't have to be Tomcat.
#!/bin/bash#--------------------------------------------------------
# Script contacts a HTTP-Server and executes
# a command if server seems to be down.
#--------------------------------------------------------
#
# Definitions on checked target
#
SERVER=localhost
PORT=8080
URL=/index.jsp
CONTENT="HTTP/1.1 200 OK"
START_CMD=/usr/java/tomcat/bin/startup.sh
RESTART_LOG=/var/log/tomcat/tomcat-restart.log
RESTART_MAILADR=calvin@hobbes.com
#
# Contact server and save server's answer
#
ANSWER=`wget --server-response --output-document=-
http://$SERVER:$PORT/$URL 2>&1`#
# Parse answer for content
#
RUNNING=`echo $ANSWER | grep "$CONTENT" | wc -l`#
# React according to running flag
#
if [ $RUNNING == 1 ]
then
echo "Server $SERVER:$PORT is running, ok."
else
DATE_STR=`date`
ERR_MSG="$DATE_STR $SERVER:$PORT is NOT running, execute start command..." # Write notification to log if log file defined
if [[ -n "$RESTART_LOG" ]]
then
echo $ERR_MSG >> $RESTART_LOG
fi # Notify admin via mail if mail address defined
if [[ -n "$RESTART_MAILADR" ]]
then
echo "The server will be restarted. Maybe you want to check
http://$SERVER:$PORT$URL" | mail -s "$ERR_MSG" $RESTART_MAILADR
fi
exec $START_CMD
fi