Quick ?s
Cheat Sheets
Man Pages
The Lynx
Software

How do I daemonize/make a daemon my perl script?

Category: Perl

To make itself into a daemon, a perl script needs to close it's current connections to STDIN, STDOUT, STDERR, chdir to a reliable space, fork itself, and setsid() itself. This needs to happen early on before any other file handles or network sockets are open. Historically you could do this manually:

use POSIX qw(setsid);

chdir '/';
umask 0;
open STDIN, '/dev/null';
open STDERR, '>/dev/null';
defined(my $pid = fork);
exit if $pid;
setsid;

... script continues.
Thankfully there is a module that automatically does this. Install "Proc::Daemon" via CPAN and all you need is one command:
use Proc::Daemon

Proc::Daemon::Init;

Yals.net is © 1999-2009 Crescendo Communications
Sharing tech info on the web for more than a decade!
This page was generated Sat May 2 21:36:43 2009