#!/bin/sh

# Author: Steve Allison
# URL: http://www.nooblet.org/
# Date: 19/07/2008 09:30
# Mail: checkprocess.sh@stalks.nooblet.org

# Checks if a process is listed as running in PS. If not,
# runs given startup script.

# Syntax: checkprocess.sh [process name] [startup script location]
# Example: checkprocess.sh mythbackend /etc/init.d/myth-backend

NUM=`ps ax|grep -v grep|grep -v ${0}|grep -c ${1}`
if [ "$NUM" = 0 ]
then
 ${2} restart 1>/dev/null 2>&1
 mail -s "checkprocess.sh - Restarted: ${1}" root < /dev/null > /dev/null
 echo "`date +"%Y%m%d.%H%M"` (${NUM}) # ${1} # Not running, restarted." >> /var/log/checkmyth.log
else
 echo "`date +"%Y%m%d.%H%M"` (${NUM}) # ${1} # Running." >> /var/log/checkmyth.log
fi
