Hi perl-people,
I'm not sure if this is beginners stuff, but I'll post here 'cause
it's the only list I'm subscribed to at the moment.
I'm writing a script that will daemonize itself, and then watch some
processes. If one of those processes die, it will start it again.
So, I've been reading the perlipc docs and I found this handy code on
proper daemonization:
use POSIX 'setsid';
sub daemonize {
#it's polite for daemons to chdir to root so that they
#don't prevent a filesystem from being unmounted
chdir '/' or die "Can't chdir to /: $!";
#it's also polite for daemons to redirect all output to
#/dev/null so users don't get random output
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>/dev/null' or die "Can't write to /dev/null:$!";
#the parent get's the new child's pid back, the child gets '0' back
defined( my $pid = fork ) or die "Can't fork: $!";
#here's where I start having problem. This code assumes that
#the parent will be exiting, thus leaving the child able
#to run setsid
exit if $pid;
setsid or die "Can't start a new session: $!";
open STDERR, '>&STDOUT' or die "Can't dup STDOUT: $!";
}
perlipc goes on to explain:
"The fork() has to come before the setsid() to ensure that you
aren't a process leader (the setsid() will fail if you are).
So, my question is, how do I implement this code WITHOUT the parent
process dieing?
--Errin
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>