File(3pm) | User Contributed Perl Documentation | File(3pm) |
Proc::PID::File - a module to manage process id files
use Proc::PID::File; die "Already running!" if Proc::PID::File->running();
Process that spawn child processes may want to protect each separately by using multiple pidfiles.
my $child1 = Proc::PID::File->new(name => "lock.1"); my $child2 = Proc::PID::File->new(name => "lock.2");
which may be checked like this:
<do-something> if $child1->alive();
and should be released manually:
$child1->release();
This Perl module is useful for writers of daemons and other processes that need to tell whether they are already running, in order to prevent multiple process instances. The module accomplishes this via *nix-style pidfiles, which are files that store a process identifier.
The module provides two interfaces: 1) a simple call, and 2) an object-oriented interface
The simple interface consists of a call as indicated in the first example of the Synopsis section above. This approach avoids causing race conditions whereby one instance of a daemon could read the pidfile after a previous instance has read it but before it has had a chance to write to it.
The parameter signature for this function is identical to that of the ->new() method described below in the OO Interface section of this document. The method's return value is the same as that of ->alive().
The following methods are provided:
This method is used to create an instance object. It automatically calls the ->file() method described below and receives the same paramters. For a listing of valid keys in this hash please refer to the aforementioned method documentation below.
In addition to the above, the following constitute valid keys:
If the parameter is not passed, no verification will take place. Please note that verification will only work for the operating systems listed below and that the OS will be auto-sensed. See also DEPENDENCIES section below.
Supported platforms: Linux, FreeBSD, Cygwin
Use this method to set the path of the pidfile. The method receives an optional hash (or hash reference) with the keys listed below, from which it makes a path of the format: $dir/$name.pid.
Returns true when the process is already running. Please note that this call must be made *after* daemonisation i.e. subsequent to the call to fork(). If the verify flag was set during the instance creation, the process id is verified, alternatively the flag may be passed directly to this method.
Causes for the current process id to be written to the pidfile.
This method is used to delete the pidfile and is automatically called by DESTROY method. It should thus be unnecessary to call it directly.
This method returns the mtime of the pidfile.
Erick Calder <ecalder@cpan.org>
1k thx to Steven Haryanto <steven@haryan.to> whose package (Proc::RID_File) inspired this implementation.
Our gratitude also to Alan Ferrency <alan@pair.com> for fingering the boot-up problem and suggesting possible solutions.
For Linux, FreeBSD and Cygwin, support of the verify option requires availability of the ps utility. For Linux/FreeBSD This is typically found in the procps package. Cygwin users need to run version 1.5.20 or later for this to work.
This module may prevent daemons from starting at system boot time. The problem occurs because the process id written to the pidfile by an instance of the daemon may coincidentally be reused by another process after a system restart, thus making the daemon think it's already running.
Some ideas on how to fix this problem are catalogued below, but unfortunately, no platform-independent solutions have yet been gleaned.
For help and thank you notes, e-mail the author directly. To report a bug, submit a patch or add to our wishlist please visit the CPAN bug manager at: http://rt.cpan.org
The latest version of the tarball, RPM and SRPM may always be found at: http://perl.arix.com/ Additionally the module is available from CPAN.
This utility is free and distributed under GPL, the Gnu Public License. A copy of this license was included in a file called LICENSE. If for some reason, this file was not included, please see http://www.gnu.org/licenses/ to obtain a copy of this license.
$Id: File.pm,v 1.16 2004-04-08 02:27:25 ekkis Exp $
2021-01-01 | perl v5.32.0 |