Pid file manipulation
use File::Pid;
my $pidfile = File::Pid->new({ file => '/some/file.pid', });
$pidfile->write;
if ( my $num = $pidfile->running ) { die "Already running: $num\n"; }
$pidfile->remove;
This software manages a pid file for you. It will create a pid file, query the process within to discover if it's still running, and remove the pid file.
\$1
my $pidfile = File::Pid->new;
my $thisfile = File::Pid->new({ file => '/var/run/daemon.pid', });
my $thisfileandpid = File::Pid->new({ file => '/var/run/daemon.pid', pid => '145', });
This constructor takes two optional paramters.
\*(C`file\*(C' - The name of the pid file to work on. If not specified, a pid file located in \*(C`File::Spec->tmpdir()\*(C' will be created that matches \*(C`(File::Basename::basename($0))[0] . '.pid'\*(C'. So, for example, if $0 is ~/bin/sig.pl, the pid file will be /tmp/sig.pl.pid.
\*(C`pid\*(C' - The pid to write to a new pidfile. If not specified, $$ is used when the pid file doesn't exist. When the pid file does exist, the pid inside it is used. my $pidfile = $pidfile->file;
Accessor/mutator for the filename used as the pid file. my $pid = $pidfile->pid;
Accessor/mutator for the pid being saved to the pid file. my $pid = $pidfile->write;
Writes the pid file to disk, inserting the pid inside the file. On success, the pid written is returned. On failure, \*(C`undef\*(C' is returned. my $pid = $pidfile->running; die "Service already running: $pid\n" if $pid;
Checks to see if the pricess identified in the pid file is still running. If the process is still running, the pid is returned. Otherwise \*(C`undef\*(C' is returned. $pidfile->remove or warn "Couldn't unlink pid file\n";
Removes the pid file from disk. Returns true on success, false on failure. This is a utility method that allows you to determine what \*(C`File::Pid\*(C' thinks the program name is. Internally this is used when no pid file is specified.
perl.
Casey West, <[email protected]>.
Copyright (c) 2005 Casey West. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.