http://qs1969.pair.com?node_id=31982

Item Description: A small but perfectly formed module for turning your unix process into a Daemon

Review Synopsis: Proc::Daemon a very useful unix utility

Proc::Daemon is a particularly useful module for anyone writing unix daemon's (a program which runs in the background with no user input). It detaches your program from the its parent, allowing it to run undisturbed by the parent, you logging out, or the parent waiting for it to die.

This is a simple module and most of the code in it is contained in The Perl Cookbook, however this module brings it together in a neatly wrapped package so you don't have to remember any of those horrid details (like the double fork, setsid, chdir, reopen STDOUT etc)!

Use it like this

use Proc::Daemon; Proc::Daemon::Init();
That is it!

You'd quite likely like to re-open STDOUT & STDERR to a log file though, like this :-

open(STDOUT, ">>$LOG") or die "Failed to re-open STDOUT to $LOG"; open(STDERR, ">&STDOUT") or die "Failed to re-open STDERR to STDOUT";
I've used it quite a few times now, for writing real daemons, sendmail filters which need to run for a long time (eg email to SMS), for detaching processes from crontab, and for long running CGI programs (which don't need to return a response to the user).

Warning: This module is unlikely to work under Windows, though it probably could be made to.

Verdict: Small but perfectly formed!

Replies are listed 'Best First'.
RE: Proc::Daemon
by gregorovius (Friar) on Sep 24, 2000 at 12:16 UTC
    > (like the double fork, setsid, chdir, reopen STDOUT etc)

    Now from the parent script you only have to do a:

    system('perl my_daemon.pl');
    and system will return as the forked off child detaches itself from the parent. Useful for when your client program has to start it's own server.

    It's Neat!