NAME
Data::Tools::Process provides set of functions for process control,
forking, daemonizing and pid files handling.
SYNOPSIS
use Data::Tools::Process qw( :all ); # import all functions
use Data::Tools::Process; # the same as :all :)
use Data::Tools::Process qw( :none ); # do not import anything
# --------------------------------------------------------------------------
# fork and exec a command, returns the child pid to the parent
my $pid = fork_exec_cmd( "/usr/bin/somecmd --with args" );
waitpid( $pid, 0 );
# --------------------------------------------------------------------------
# detach from the controlling terminal and become a daemon
daemonize();
daemonize( CHDIR => '/var/lib/myapp', UMASK => 0022 );
daemonize( STDOUT => '/var/log/myapp.log', STDERR => '/var/log/myapp.err' );
daemonize( CLOSE_ALL_FDS => 1 );
# the same, but stay in the current directory
daemonize_here( STDOUT => 'myapp.log', STDERR => 'myapp.log' );
# --------------------------------------------------------------------------
# create a pid file, holding the pid of the current process
my $res = pidfile_create( '/var/run/myapp.pid' );
die "already running with pid [$res]" if $res;
# ...and take over the pid file if the process in it is gone
my $res = pidfile_create( '/var/run/myapp.pid', STALE_CHECK => 1 );
# signal the process named in a pid file and remove the file
pidfile_kill_and_remove( '/var/run/myapp.pid' ); # sends TERM
pidfile_kill_and_remove( '/var/run/myapp.pid', 15, '5s', 9 );
# just remove the pid file
pidfile_remove( '/var/run/myapp.pid' );
# --------------------------------------------------------------------------
# check if the current directory was removed under the process
die "current directory is gone" if cwd_gone();
# the same for any path
print "log dir is gone\n" if path_gone( '/var/log/myapp' );
# --------------------------------------------------------------------------
FUNCTIONS
fork_exec_cmd( $command )
Forks and exec()s $command in the child process.
Returns:
* the child process pid in the parent process
* undef if fork() failed
The child never returns. $command is passed to exec() as a single string, so it is subject to the usual shell handling of exec().
daemonize( %options )
Detaches the current process from the controlling terminal and turns it into a daemon: forks, calls POSIX::setsid(), forks again (SVR4 second fork policy), changes the current directory, closes STDIN, STDOUT and STDERR (and all other open file descriptors if CLOSE_ALL_FDS is set) and reopens them as file descriptors 0, 1 and 2. STDIN is always /dev/null, STDOUT and STDERR are /dev/null unless files are given for them.
Options are:
CHDIR => $path # directory to change to, default is '/'
UMASK => $umask # umask to set, default is 0077
STDOUT => $file # file for STDOUT, default is /dev/null
STDERR => $file # file for STDERR, default is /dev/null
CLOSE_ALL_FDS => 1 # close all open file descriptors, default is
# to close only 0, 1 and 2, see below
STDOUT and STDERR files are opened for append and created if missing, with permissions limited by UMASK (0600 with the default). Relative file names are resolved after the change to CHDIR, so with the default CHDIR '/' use full paths (or daemonize_here()). STDOUT and STDERR may name the same file. STDOUT is buffered, set autoflush on it if log lines must show up immediately.
CLOSE_ALL_FDS => 1 closes file descriptors up to the system limit (sysconf _SC_OPEN_MAX), but not above 1024, since under a high open files limit (ulimit -n) closing them one by one can take a long time. A value from 2 to 1024 (e.g. CLOSE_ALL_FDS => 2) closes up to the system limit uncapped. A value above 1024 is used as the cap instead of 1024, e.g. CLOSE_ALL_FDS => 65536 closes up to the system limit, but not above 65536.
Returns 1 in the resulting daemon process. The original process and the intermediate one leave with POSIX::_exit(0) and never return, so END blocks and object destructors are not run in them (they run only in the daemon, when it exits). Dies on any of the steps failing.
NOTE: the original process exits (with status 0) before STDOUT/STDERR
files are opened, and STDERR is already closed when they are, so a
bad STDOUT or STDERR file name (missing directory, no permissions)
makes the daemon die() silently: nothing is printed anywhere and
the caller sees success. check that the files can be opened before
calling daemonize(), for example open them for append and close
them, using the same names as they will be resolved after CHDIR.
NOTE: without CLOSE_ALL_FDS all file descriptors other than 0, 1 and 2 are
inherited by the daemon, close the ones it must not hold (pipes,
locks, sockets). with CLOSE_ALL_FDS all open file descriptors are
closed, including any files, sockets or database handles opened
before the call, and perl handles to them must not be used anymore.
daemonize_here( %options )
The same as daemonize() but the CHDIR default is the current working directory instead of '/', so the daemon stays where it was started and relative STDOUT/STDERR file names are relative to it. All daemonize() options are accepted, an explicit CHDIR overrides the default.
pidfile_create( $pid_file_name, %options )
Creates $pid_file_name and writes the pid of the current process in it. The file is created with O_EXCL, so two processes racing for the same pid file cannot both succeed. Missing directories on the way are created.
Options are:
STALE_CHECK => 1 # take over the pid file if its process is not running
Returns:
* undef on success, the pid file now holds our pid
* a positive pid if the pid file exists and holds a running process
* -1 if the pid file cannot be created
A pid file which does not hold a valid (positive) pid is always considered stale and is replaced, regardless of STALE_CHECK.
pidfile_kill_and_remove( $pid_file_name, @signal_list )
Sends the given signals to the pid found in $pid_file_name and then removes the file. @signal_list defaults to a single TERM (15).
A list item ending with 's' is not a signal but a sleep time in seconds. All items are processed in the given order, so:
pidfile_kill_and_remove( $fname, 15, '5s', 15, '2s', 9 );
sends TERM, waits 5 seconds, sends TERM again, waits 2 seconds, sends KILL.
Returns:
* undef if $pid_file_name does not exist
* 1 if the signals have been sent and the pid file removed
pidfile_remove( $pid_file_name )
Removes $pid_file_name. Returns undef.
path_gone( $path )
Checks if $path is gone. Returns true if $path cannot be stat()-ed (it does not exist, or the stat fails with ESTALE or similar on a network file system), or if its link count is 0 (it was unlinked). Returns false if $path is still there.
Note that a link count of 0 can only be seen through something still holding the removed entry, like the current directory ('.') of a process whose directory was removed. A removed path given by name just fails stat().
cwd_gone()
The same as path_gone( '.' ): returns true if the current working directory of the process was removed (or is no longer accessible on a network file system), false otherwise. Useful for long running processes and daemons started with daemonize_here(), whose directory may be removed under them.
REQUIRED MODULES
Data::Tools::Process uses:
* POSIX
* Data::Tools
GITHUB REPOSITORY
git@github.com:cade-vs/perl-data-tools-process.git
git clone git://github.com/cade-vs/perl-data-tools-process.git
AUTHOR
Vladi Belperchinov-Shabanski "Cade"
<cade@noxrun.com> <cade@bis.bg> <cade@cpan.org>
http://cade.noxrun.com/