supervisor manage program with daemon

dhcpd cmd:/usr/sbin/dhcpd -q -cf /etc/dhcp/dhcpd.conf -pf /var/run/dhcpd.pid eth0

supervisor log will tell: exited: dhcpd (exit status 1; not expected)

because the dhcpd is a daemon process and it will run in background while start, so supervisor will restart the process util over the retry times.

SUPERVISOR记录
foreman-base/supervisord.conf
daemon proxy

dhcpd.conf:

[program:dhcpd_proxy]
directory=/etc/supervisor/conf.d
command=bash foreground_dhcp.sh
stderr_logfile=/var/log/supervisor/dhcpd-stderr.log
stdout_logfile=/var/log/supervisor/dhcpd-stdout.log
user=root
priority=2
autostart=true
autorestart=true
;autorestart=unexpected

foreground_dhcp.sh:

#! /usr/bin/env bash
set -eu

pidfile="/run/dhcpd.pid"
command="/usr/sbin/dhcpd -q -cf /etc/dhcp/dhcpd.conf"
stdout="/var/log/supervisor/dhcpd-stdout.log"

# Proxy signals
function kill_app(){
 kill $(cat $pidfile)
 exit 0 # exit okay
}
trap "kill_app" SIGINT SIGTERM

echo `date`': restart the foreground dhcpd-proxy process !!!' >> $stdout
# kill the dhcpd process before start
if [ -f $pidfile ] && kill -9 $(cat $pidfile); then
 echo `date`': kill dhcpd process success, because the foreground dhcpd-proxy process was killed !' >> $stdout
else
 echo `date`': kill dhcpd process fail, because the dhcpd daemon process was killed !' >> $stdout
fi

# for release the process, the same as below
sleep 2
# Launch daemon
$command
sleep 2

# Loop while the pidfile and the process exist
while [ -f $pidfile ] && kill -0 $(cat $pidfile) ; do
 sleep 0.5
done
exit 1000 # exit unexpected

Leave a Reply

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