Proc::Daemon - Run Perl program(s) as a daemon process
use Proc::Daemon; $daemon = Proc::Daemon->new( work_dir => '/my/daemon/directory', ..... ); $Kid_PID = $daemon->Init; unless ( $Kid_PID ) { # code executed only by the child ... } $Kid_PID2 = $daemon->Init( { work_dir => '/other/daemon/directory', exec_command => 'perl /home/my_script.pl', } ); $pid = $daemon->Status( ... ); $stopped = $daemon->Kill_Daemon( ... );
This module can be used by a Perl program to initialize itself as a daemon or to execute (exec) a system command as daemon. You can also check the status of the daemon (alive or dead) and you can kill the daemon.
exec
A daemon is a process that runs in the background with no controlling terminal. Generally servers (like FTP, HTTP and SIP servers) run as daemon processes. Do not make the mistake to think that a daemon is a server. ;-)
Proc::Daemon does the following:
The script forks a child.
The forked child becomes a session leader, which detaches the program from the controlling terminal.
This child forks another child (the final daemon process). This prevents the potential of acquiring a controlling terminal at all.
The second child changes the current working directory to the value of 'work_dir'.
The second child clears the file creation mask.
The second child closes all open file descriptors.
The second child opens STDIN, STDOUT and STDERR to the location defined in the constructor (new).
new
The second child returns to the first process (grandparent), or the program defined in 'exec_command' is executed and the second child never returns.
The first child transfers the PID of the second child (daemon) to the parent and exits. Additionally the PID of the daemon process can be written into a file if 'pid_file' is defined.
The constructor creates a new Proc::Daemon object based on the hash %ARGS. The following keys from %ARGS are used:
Defines the path to the daemon working directory. Defaults to /.
/
Defines the path to STDIN of your daemon. Defaults to /dev/null.
/dev/null
Defines the path where the output of your daemon will go. Defaults to *\STDIN.
*\STDIN
Defines the path where the error output of your daemon will go. Defaults to *\STDIN.
Defines the path to the file where the PID of the child process is stored. Defaults to undef (= write no file).
undef
Scalar (or arrayref) with system command(s) that will be executed by the daemon via Perls exec PROGRAM_LIST. In this case the child will never return to the parents process!
exec PROGRAM_LIST
Example:
my $daemon = Proc::Daemon->new( work_dir => '/working/daemon/directory', child_STDOUT => '/path/to/daemon/output.file', child_STDERR => 'debug.txt', pid_file => 'pid.txt', exec_command => 'perl /home/my_script.pl', # or # exec_command => [ 'perl /home/my_script.pl', 'perl /home/my_other_script.pl' ], );
In this example:
the PID of the daemon will be returned into $daemon of the parent process and a pid-file will be created at /working/daemon/directory/pid.txt
$daemon
/working/daemon/directory/pid.txt
all STDOUT of the daemon will go to /path/to/daemon/output.file and all STDERR will go to /working/daemon/directory/debug.txt
/path/to/daemon/output.file
/working/daemon/directory/debug.txt
the script /home/my_script.pl will be executed by perl and run as daemon. Therefore the child process will never return to this parent script.
/home/my_script.pl
perl
Become a daemon.
If used for the first time after new, you call Init with the object reference to start the daemon.
Init
$pid = $daemon->Init();
If you want to use the object reference created by new for other daemons, you write Init( { %ARGS } ). The argument must be a hashref. %ARGS are the same as described in new. Notice that you shouldn't call Init(), or this next daemon will do and write all in the same files as the first daemon. At least use an empty anonymous hash.
Init( { %ARGS } )
Init()
$pid = $daemon->Init( {} ); @pid = $daemon->Init( { work_dir => '/other/daemon/directory', exec_command => [ 'perl /home/my_second_script.pl', 'perl /home/my_third_script.pl' ], } );
If you don't need the Proc::Daemon object reference in your script, you can also use the method without object reference:
$pid = Proc::Daemon::Init( [ { %ARGS } ] );
If you call the Init method in the context without looking for a return value (void context) the parent process will exit here like in earlier versions:
exit
Proc::Daemon::Init;
This function checks the status of the process (daemon). Returns the PID number (alive) or 0 (dead).
$ARG can be a string with:
undef, in this case it tries to get the PID to check out of the object reference settings.
a PID number to check.
the path to a file containing the PID to check.
the command line entry of the running program to check. This requires Proc::ProcessTable to be installed.
This function kills the Daemon process. Returns the number of processes successfully killed (which mostly is not the same as the PID number), or 0 if the process wasn't found.
$ARG is the same as of Status().
Status()
Is like the Perl built-in fork, but it retries to fork over 30 seconds if necessary and if possible to fork at all. It returns the child PID to the parent process and 0 to the child process. If the fork is unsuccessful it warns and returns undef.
fork
warn
Proc::Daemon also defines some other functions. See source code for more details:
Returns the maximum file descriptor number. If undetermined, $NUMBER (if undefined: 64) will be returned.
Does some fixes/adjustments on the new settings together with fix_filename.
fix_filename
Prevents double use of same filename in different processes.
Returns the wanted PID if it can be found.
Returns the wanted PID by looking into the process table, or undef. Requires the Proc::ProcessTable module to be installed.
Proc::ProcessTable
Proc::Daemon::init is still available for backwards capability.
Proc::Daemon::init
Primary-maintainer and code writer until version 0.03:
Earl Hood, earl@earlhood.com, http://www.earlhood.com/
Co-maintainer and code writer since version 0.04:
Detlef Pilzecker, http://search.cpan.org/~deti/, http://www.secure-sip-server.net/
Initial implementation of Proc::Daemon derived from the following sources:
Proc::Daemon
Advanced Programming in the UNIX Environment, by W. Richard Stevens. Addison-Wesley, Copyright 1992.
UNIX Network Progamming, Vol 1, by W. Richard Stevens. Prentice-Hall PTR, Copyright 1998.
This module requires the POSIX module to be installed.
POSIX
The Proc::ProcessTable module is not essentially required but it can be usefull if it is installed (see above).
perl(1), POSIX, Proc::ProcessTable
This module is Copyright (C) 1997-2010 by Earl Hood and Detlef Pilzecker.
All Rights Reserved.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
To install Proc::Daemon, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Proc::Daemon
CPAN shell
perl -MCPAN -e shell install Proc::Daemon
For more information on module installation, please visit the detailed CPAN module installation guide.