Quick ?s
Cheat Sheets
Man Pages
The Lynx

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;

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


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