> 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.
> 

A stretch, but there have been more complex topics discussed. This is
pretty much a catch all and some of the experts will probably appreciate
not answering the same how do I delete an element of an array question :-).

> 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:
> 

Great docs... however...

> 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
> 

Actually you want the parent process to "die"... or at least exit, then
your child process is the session leader and will do everything else you
need, aka fork more children in your case. Essentially you want to make
it so that the tty/shell (whatever) no longer thinks it has absentee
children so that it can exit cleanly, by daemonizing your process
essentially becomes a child of the kernel, which will always be running
(until shutdown that is).

However, the best advice NOT given by perldoc perlipc is to use the
Proc::Daemon::Init module, it is incredibly easy and takes care of all
of the code you mention above by itself, in a neat package that you
don't have to worry about the internals of.  It is available through
CPAN as usual, and despite its incredibly "low release number" (if you
are into that hole thing) I have never had problems with it.

Two additional comments, that you are likely to stumble across:

1. How do I get back to my process after it has daemonized, how do I
stop it, etc?  

This is where you will likely want to get into writing a "pid" file to
the disk so that your process can be looked up and signaled, and/or to
prevent two copies of your daemon running, or at least trying to work on
teh same resources.

2. How do I do logging, etc.?  

Check out the very, very excellent Log::Log4perl suite, it is incredible
and can accomplish just about all of your logging needs.

Of course I have already mentioned POE.... but I will plug it again.

http://danconia.org

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to