Erik E. Fair
2013-12-04 06:40:54 UTC
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
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