Skip to content

Instantly share code, notes, and snippets.

@vijayanandnandam
Created June 13, 2014 02:51
Show Gist options
  • Save vijayanandnandam/5ddbd470b124f295d70b to your computer and use it in GitHub Desktop.
Save vijayanandnandam/5ddbd470b124f295d70b to your computer and use it in GitHub Desktop.
Program monitor
#!/bin/bash
#
# watchdog
#
# Run as a cron job to keep an eye on what_to_monitor which should always
# be running. Restart what_to_monitor and send notification as needed.
#
# This needs to be run as root or a user that can start system services.
#
# Revisions: 0.1 (20100506), 0.2 (20100507)
NAME=$1
START=/etc/init.d/$NAME start
[email protected]
[email protected]
GREP=/bin/grep
PS=/bin/ps
NOP=/bin/true
DATE=/bin/date
MAIL=/bin/mail
RM=/bin/rm
$PS -ef|$GREP -v grep|$GREP $NAME >/dev/null 2>&1
case "$?" in
0)
# It is running in this case so we do nothing.
$NOP
;;
1)
echo "$NAME is NOT RUNNING. Starting $NAME and sending notices."
$START 2>&1 >/dev/null &
NOTICE=/tmp/watchdog.txt
echo "$NAME was not running and was started on `$DATE`" > $NOTICE
$MAIL -n -s "watchdog notice" -c $NOTIFYCC $NOTIFY < $NOTICE
$RM -f $NOTICE
;;
esac
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment