Proc::Daemon
Run Perl program(s) as a daemon process.
- Provided by: libproc-daemon-perl (Version: 0.23-1)
- Report a bug
Run Perl program(s) as a daemon process.
use Proc::Daemon;
$daemon = Proc::Daemon->new(
work_dir => '/my/daemon/directory',
.....
);
$Kid_1_PID = $daemon->Init;
unless ( $Kid_1_PID ) {
# code executed only by the child ...
}
$Kid_2_PID = $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.
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:
NOTE: Because of the second fork the daemon will not be a session-leader and therefore Signals will not be send to other members of his process group. If you need the functionality of a session-leader you may want to call POSIX::setsid() manually at your daemon.
INFO: Since "fork" is not performed the same way on Windows systems as on Linux, this module does not work with Windows. Patches appreciated!
dont_close_fh => [ 'main::DATA', 'PackageName::DATA', $my_filehandle, ... ],
You can add any kind of file handle to the array (expression in single quotes or a scalar variable), including 'STDIN', 'STDOUT' and 'STDERR'. Logically the path settings from above ("child_STDIN", ...) will be ignored in this case.
DISCLAIMER: Using this argument may not detach your daemon fully from the parent! Use it at your own risk.
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:
If used for the first time after "new", you call "Init" with the object reference to start the daemon.
$pid = $daemon->Init();
If you want to use the object reference created by "new" for other daemons, you write "Init( { %ARGS } )". %ARGS are the same as described in "new". Notice that you shouldn't call "Init()" without argument in this case, or the next daemon will execute and/or write in the same files as the first daemon. To prevent this use at least an empty anonymous hash here.
$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();
# or
$pid = Proc::Daemon::Init( { %ARGS } );
"Init" returns the PID (scalar) of the daemon to the parent, or the PIDs (array) of the daemons created if "exec_command" has more then one program to execute. See examples above.
"Init" returns 0 to the child (daemon).
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:
Proc::Daemon::Init();
$ARG can be a string with:
$ARG is the same as of "Status()". SIGNAL is an optional signal name or number as required by Perls "kill" function and listed out by "kill -l" on your system. Default value is 9 ('KILL' = non-catchable, non-ignorable kill).
Proc::Daemon also defines some other functions. See source code for more details:
"Proc::Daemon::init" is still available for backwards capability.
Proc::Daemon is now taint safe (assuming it is not passed any tainted parameters).
Primary-maintainer and code writer until version 0.03:
Co-maintainer and code writer since version 0.04 until version 0.14:
Co-maintainer and code writer since version 0.15:
Initial implementation of "Proc::Daemon" derived from the following sources:
This module requires the "POSIX" module to be installed.
The "Proc::ProcessTable" module is not essentially required but it can be useful if it is installed (see above).
<https://github.com/akreal/Proc-Daemon>
perl(1), POSIX, Proc::ProcessTable
This module is Copyright (C) 1997-2015 by Earl Hood, Detlef Pilzecker and Pavel Denisov.
All Rights Reserved.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.