Hello folks,
I'm writing a custom daemon (test platform is CentOS 5.5) that needs to run at startup and respawn if it ever terminates for whatever reason. I've scrounged around on the net and found some advice for how to achieve this but I've had no success.
I've tried:
- Adding my executable to inittab as gt:345:respawn:/sbin/goose-daemon In this case 8 copies of the daemon process are spawned and init stops further copies from opening for a while
- Removing the daemon(0,0) calls from the program. In this case as expected init reaches the main loop in the program and thus never returns to carry on with loading other scripts.
- Adding a startup script to init.d and using chkconfig to symlink it into runlevels 345 (the daemon(0,0) call is added back in). In this case the daemon starts correctly, terminates correctly, but when I call pkill goose-daemon the process does not relaunch.
According to various websites (
this one in particular) starting the process directly from inittab with respawn set should work but it doesn't. What am I missing here? Do I need some particular set of code to interact properly with init to allow the both the correct respawn of the process as well as deamonizing it? Is there some way to use the rc?.d files to respawn the process if it is killed?
Below is the code I am using to daemonize the process as well as part of the of the script for rc?.d
Code:
daemon(0,0);
signal(SIGCHLD,SIG_IGN); /* ignore child */
signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGHUP,SignalHandler); /* catch hangup signal */
signal(SIGTERM,SignalHandler); /* catch kill signal */
Code:
function start()
{
echo -n $"Starting goose-daemon:"
daemon $PROCESS $CONFIG
echo
RETVAL=$?
if [ $RETVAL -eq 0 ] ; then
touch $LOCKFILE
fi
}
function stop()
{
action $"Stopping goose-daemon:" killproc goose-daemon
RETVAL=$?
if [ $RETVAL -eq 0 ] ; then
rm -f $LOCKFILE
fi
}