Daemonizing perl code: fork() is your friend! :-)

From: Bjarni R. Einarsson (
Date: Wed 09 Feb 2005 - 19:38:56 GMT

On 2005-02-09, 10:46:00 (-0500), Derrick Webber wrote:
> There are problems daemonizing code that wasn't written to stay in memory.
> Global variables are assumed to be empty, unused memory is not released,
> etc. (folks who port stuff to mod_perl are very familiar with the issues).
> Perperl uses a few tricks to limit the effects of this, but it's not as
> good as code designed from the start to run as a daemon.
> Sanitizer may not have these issues... only real way to know is to try it.

I've daemonized stuff which includes the sanitizer, for work.
I haven't received permission to release and open that code
yet... but I can tell you how I avoided these problems:

I simply fork() before doing anything interesting.

All the heavy lifting takes place in the child processes,
thus protecting the parent from any crashes/hangs and
eliminating memory issues completely. This is easy to do
with the sanitizer, since it expects both it's input and
output to be file descriptors, whether those are network
sockets or stdin/stdout or pipes doesn't matter and all such
descriptors are inherited by childern after a fork().

Bjarni Rúnar Einarsson

PGP: 02764305, B7A3AB89

hosted by