|
NAMENo::Worries::PidFile - pid file handling without worriesSYNOPSISuse No::Worries::PidFile qw(*); # idiomatic daemon code pf_set($pidfile); while (1) { ... $action = pf_check($pidfile); last if $action eq "quit"; pf_touch($pidfile); ... } pf_unset($pidfile); # idiomatic daemon code with sleeping pf_set($pidfile); while (1) { ... pf_sleep($pidfile, time => 5) or last; ... } pf_unset($pidfile); # here is how to handle a --status option if ($Option{status}) { ($status, $message, $code) = pf_status($pidfile, freshness => 10); printf("myprog %s\n", $message); exit($code); } # here is how to handle a --quit option if ($Option{quit}) { pf_quit($pidfile, linger => 10, callback => sub { printf("myprog %s\n", $_[0]) }, ); } DESCRIPTIONThis module eases pid file handling by providing high level functions to set, check, touch and unset pid files. All the functions die() on error.The pid file usually contains the process id on a single line, followed by a newline. However, it can also be followed by an optional action, also followed by a newline. This allows some kind of inter-process communication: a process using pf_quit() will append the "quit" action to the pid file and the owning process will detect this via pf_check(). All the functions properly handle concurrency. For instance, when two processes start at the exact same time and call pf_set(), only one will succeed and the other one will get an error. Since an existing pid file will make pf_set() fail, it is very important to remove the pid file in all situations, including errors. The recommended way to do so is to use an END block: # we need to know about transient processes use No::Worries::Proc qw(); # we need to record what needs to be cleaned up our(%NeedsCleanup); # we set the pid file here and remember to clean it up pf_set($pidfile); $NeedsCleanup{pidfile} = 1; # ... anything can happen here ... # cleanup code in an END block END { # transient processes do not need cleanup return if $No::Worries::Proc::Transient; # cleanup the pid file if needed pf_unset($pidfile) if $NeedsCleanup{pidfile}; } FUNCTIONSThis module provides the following functions (none of them being exported by default):
SEE ALSO<http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html>, No::Worries, No::Worries::Proc.AUTHORLionel Cons <http://cern.ch/lionel.cons>Copyright (C) CERN 2012-2019
Visit the GSP FreeBSD Man Page Interface. |