Okay, I was very proud to have created an application that makes itself a daemon (background, no related console, etc.).
This has been working great. What I want to do now is to have it automatically startup upon boot-up, and restart whenever it dies, so I put this into my /etc/inittab:
m1:12345:respawn:/nerr/src/start_script
in start_script are these lines:
#!/bin/sh
/nerr/src/remora/remora
/nerr/src/comm/scratch/marisys_comm ysi.cfg
/nerr/src/comm/scratch/marisys_comm cr10.cfg
'remora' and 'marisys_comm' are each daemons.
However when I reboot, I get 'remora' processes all over the place, and 3 marisys_comm processes, one that says <defunct>.
Since each of these applications creates 3 processes, and the first
two die (to remove any associated console, etc.), I imagine the OS thinks the application died, so it respawns it, when in actuality there's a ("sub") process that's the real intended process, still running and doing what it's supposed to.
Any ideas what I'm doing wrong?
My daemon code seems to have been working fine, but here it is for reference, in case I'm doing something stupid (not a first or a last!):
Code:
///////////////////////////////////////////////////////////////////////////////
//
// Procedure: MakeMyselfaDaemon
//
// Purpose: This will make our current process a daemon
//
// Returns: 0 always
//
// Notes: If forking off children fails, exit() will be called, not return()!
//
// We currently do NOT chdir, so whatever directory we are launched from will not
// be able to be unmounted!
//
//////////////////////////////////////////////////////////////////////////////
int MakeMyselfaDaemon()
{
pid_t myPID; // my pocess ID
// try and create a child then kill ourselves
if ((myPID = fork()) == -1)
{
// we could not fork, do something
cout << "\r\nOur parent could not create an additional process: fork() failed\r\n";
exit(1);
} // end if
else if (myPID != 0)
{
// we are the parent so sleep then die
// give our child time to execute....
sleep(2);
exit (1);
} // end else if
else
{
// now try to run our child in its own process group without any terminal
if (setsid() == -1)
{
// we could not create our own process group or maybe ourr child cannot be theleader?
cout << "\r\nWe could not disassociate from the terminal: setsid() returned -1\r\n";
_exit(1);
} // end if
} // end else
// if we're here than we're our parent's child and our parent killed itself
// now fork again so our child (session group leader) can exit
if ((myPID = fork()) == -1)
{
/* we could not fork, do something */
cout << "\r\nOur first child could not create an additional process: fork() failed\r\n";
_exit(1);
} // end if
else if (myPID != 0)
{
// we are the session group leader so sleep then die
// give our second child time to execute
sleep(2);
_exit(1);
} // end else if
// we are now the second child, and no longer a session group leader
/////////////////////////////////////////////////////////////////////////////////////////
// NOTE: if we ever want to unmount the directory we were run from, we must chdir here //
/////////////////////////////////////////////////////////////////////////////////////////
// make sure we have full write permissions, since we don't know WHO invoked us...
umask(0);
// now close all of our std streams
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
return (0);
} // MakeMyselfaDaemon()
Thanks for any tips.