#!/bin/bash
#
# Author: Yee Hsu
# Date: 6/10/2010
#
# Watchdog bash script to determine if a particular process is running.
# If not, it will start up the process. Ideal for monitoring services
# that must be running 24x7.
#
# Should schedule a cron job to periodically execute this script
# to check service uptime.
#
# Enter an entry in crontab to run, example every minute...
# $ crontab -l
# $ crontab -e
#
# Add the line...
# */1 * * * * /home/user/watchdog.sh
#
# (Use to post in the top of your crontab)
# ------------- minute (0 - 59)
# | ----------- hour (0 - 23)
# | | --------- day of month (1 - 31)
# | | | ------- month (1 - 12)
# | | | | ----- day of week (0 - 6) (Sunday=0)
# | | | | |
# * * * * * command to be executed
#
# Copyright (c) Tim Hsu 2009. All rights reserved.
# ===========================================================================
# some global variables
SERVER="Linux Server"
EMAILF="daemon@domain.com.hk"
EMAILT="sysconsole@domain.com.hk"
# void CheckProcess(ProcessName, ProcessExecPath)
function CheckProcess
{
PROCNAME=$1
PROCPATH=$2
RUNNING=`ps -e | grep $PROCNAME | wc -l`
if [[ $RUNNING -eq 0 ]] ; then
echo "$PROCNAME stopped, attempting to restart..."
/bin/mail $EMAILT -r $EMAILF -s "Service [$PROCNAME] Down on [$SERVER]. Restarting." < /dev/null
if [[ -f $PROCPATH ]] ; then
ulimit -n 8192 # increase fd
ulimit -s 2048 # decrease stack size
$PROCPATH & # run process in background
else
/sbin/service $PROCPATH restart # start service
fi
fi
}
function main
{
# checks the service to see if it is running, if not, start it
CheckProcess mysqld mysql
# checks the process to see if it is running, if not, start it
CheckProcess procname "/home/bin/main/src/procname"
}
main;
exit;