A simple watchdog script

At times you find some daemons just stop but since they’re not actively used all the time etc you don’t find out until long after. This script watches the named process. Run it from cron to run checks occasionally (like */15 * * * * for every 15 minutes).

If you replace the

DAEMON=<daemon name>

with

DAEMON=$1

You can run the daemon name as parameter and use this script to check multiple daemons.

#!/bin/bash
# Check for the status of a daemon (with systemd)
# Can be altered to accept daemon's name as command line option
###
# (c)opyleft Take13

# Set your mail address and the name of the process to watch (.service postfix is not necessary).
MAILTO=invaderzim@take13.net
# Example is set to check ntpd. Comment this line and uncomment next to 
# use command line option for daemon name
DAEMON=ntpd
#DAEMON=$1

# No need to edit
STATUS=`/usr/bin/systemctl status $DAEMON | grep Active | awk '{print $2}'`
PID=`/usr/bin/systemctl status $DAEMON | grep 'Main PID' | awk '{print $3}'`
SINCE=`/usr/bin/systemctl status $DAEMON | grep Active | cut -b 19-`
SUBJECT=""

mail_error () {
	( echo "Subject: "$SUBJECT ; echo $STATUS" since "$SINCE ) | sendmail $MAILTO
	}


case $STATUS in
	active) echo "All is well. PID=$PID"
		exit
		;;
	inactive)	echo "Stopped. Restarting "$DAEMON"."
#		/usr/bin/systemctl start $DAEMON
		SUBJECT=$DAEMON" stopped on "`hostname`"."
		mail_error
		;;
	failed)	echo "Failed since "$SINCE
		echo "Restarting"
#		/usr/bin/systemctl restart $DAEMON
		SUBJECT=$DAEMON" failed on "`hostname`"."
		mail_error
		;;
esac

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

one + 20 =