I run this script on my servers every fifteen minutes from cron:
Code:
#!/bin/sh
LOG=/var/log/daemon-check.log
PROGS="sshd crond rsyslog httpd named ntpd [more daemons if appropriate]"
for prog in $PROGS
do
progtest=$(ps aux --width=256 | grep $prog | grep -v grep)
if [ "$progtest" != "" ]
then
echo -n `date` >> $LOG
echo " $prog running" >> $LOG
else
/sbin/service $prog restart >> $LOG 2>&1
echo -n `date` >> $LOG
echo " $prog *** RESTARTED ***" >> $LOG
fi
done
The key line is the one that creates "progtest" by using "ps aux".
This script is designed to restart daemons that have stopped for some reason. You might also want to receive email notifications if a daemon is restarted. If the machine has an SMTP listener running like Postfix or sendmail, add the line
Code:
echo "Restarted $prog at $(date)" | mail -s "$prog RESTARTED!" you@example.com
just after the line that logs the restart, replacing "you@example.com" with your correct email address.
In PHP, you can run the "ps aux" command using the shell_exec() function.
Bookmarks