Skip to content

Instantly share code, notes, and snippets.

@mvargeson
Created April 24, 2014 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvargeson/11236731 to your computer and use it in GitHub Desktop.
Save mvargeson/11236731 to your computer and use it in GitHub Desktop.
Watchdog script that is meant to run in most *nix environments
#!/bin/bash
#
# Watchdog Script
#
# This script is meant to be run as a cron job. This needs to be run as root or a user
# that can start system services.
#
# It looks to see if $NAME is running. If $NAME is not running then it attempts to start
# the process and send notifications.
#
# Original script found at
# http://blog.eracc.com/2010/05/08/linux-monitor-a-service-with-a-watchdog-script/
NAME=processd
START=/path/to/start/$NAME
NOTIFY=person1@example.com
NOTIFYCC=person2@example.com
PS=`which ps`
GREP=`which grep`
NOP=`which true`
DATE=`which date`
MAIL=`which mail`
RM=`which rm`
$PS -ef|$GREP -v grep|$GREP $NAME >/dev/null 2>&1
case "$?" in
0)
$NOP
;;
1)
NOTICE=/tmp/watchdog.txt
echo "[`$DATE`] $NAME is not running" | tee $NOTICE
echo "[`$DATE`] Attempting to start $NAME" | tee -a $NOTICE
if $START 2>&1 >/dev/null
then
echo "[`$DATE`] $NAME has been successfully started" | tee -a $NOTICE
else
echo "[`$DATE`] $NAME has failed to start" | tee -a $NOTICE
fi
$MAIL -n -s "$NAME Process 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