Discussion:
cron script to monitor whether daemons are running
Erik E. Fair
2013-12-04 06:40:54 UTC
Permalink
Sometimes essential daemons die when they should not. Without a
parent watcher process of some kind constantly lying in wait(2),
someone has to notice and restart them. That is the genesis of the
following "keepalive" shell script. It may be of some use to some
of you.

Erik <***@netbsd.org>

#!/bin/sh
# Monitor the existence of daemons with .pid files and restart as necessary.
#
# This script depends on the NetBSD convention that a daemon, its
# PID file, and its rc.d(8) control script all have the same name,
# and that it will be run as root by cron(8) as frequently as the
# system administrator deems necessary, e.g.
# */5 * * * * /usr/local/sbin/keepalive named ntpd sshd
#
# Erik Fair <***@netbsd.org>, Nov 26 2013

PIDdir=/var/run

cd /tmp

for daemon do
if [ -d ${PIDdir}/${daemon} ]; then
PIDfile=${PIDdir}/${daemon}/${daemon}.pid
else
PIDfile=${PIDdir}/${daemon}.pid
fi

if [ -f ${PIDfile} ]; then
if shlock -f ${PIDfile}; then
# echo ${daemon} is alive
else
# echo ${daemon} is dead
/etc/rc.d/${daemon} restart
fi
else
echo ${daemon} has no ${PIDfile}
fi
done
Aleksej Saushev
2013-12-04 08:37:49 UTC
Permalink
Hello,

This is important topic, it is nice that you have brought it up.
Post by Erik E. Fair
Sometimes essential daemons die when they should not. Without a
parent watcher process of some kind constantly lying in wait(2),
someone has to notice and restart them. That is the genesis of the
following "keepalive" shell script. It may be of some use to some
of you.
Wouldn't it be better to support something like daemontools or monit?
Post by Erik E. Fair
#!/bin/sh
# Monitor the existence of daemons with .pid files and restart as necessary.
"status" command reports "disabled", "running", and "not running".
Sure, the output is a bit inconvenient for automation.
Perhaps we could improve it.
--
BCE HA MOPE!
Loading...