CTK::FilePid - The Pid File simple interface
Version 1.05
use CTK::FilePid; my $pidfile = CTK::FilePid->new ({ file => '/some/file.pid', }); if ( my $num = $pidfile->running ) { die "Already running: $num"; } else { $pidfile->write; # ... $pidfile->remove; }
... or with autoremove:
my $pidfile = CTK::FilePid->new ({ file => '/some/file.pid', autoremove => 1, }); if ( my $num = $pidfile->running ) { die "Already running: $num"; } else { $pidfile->write; # ... }
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.
This module is based on File::Pid module
my $pidfile = CTK::FilePid->new; my $thisfile = File::Pid->new({ file => '/var/run/daemon.pid', }); my $thisfileandpid = CTK::FilePid->new({ file => '/var/run/daemon.pid', pid => '145', autoremove => 1, });
This constructor takes three optional paramters.
file - The name of the pid file to work on. If not specified, a pid file located in rundir(). So, for example, if $0 is ~/bin/sig.pl, the pid file will be /var/run/sig.pl.pid.
file
rundir()
$0
pid - 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.
pid
$$
autoremove - Auto-remove flag. If this flag specified as true, then will be removed the pid file automatically on DESTROY phase. Default: false
autoremove
$pidfile->file("/var/run/file.pid"); my $pidfile = $pidfile->file;
Accessor/mutator for the filename used as the pid file.
$pidfile->pid(123); my $pid = $pidfile->pid;
Accessor/mutator for the pid being saved to the pid file.
$pidfile->remove or warn "Couldn't unlink pid file";
Removes the pid file from disk. Returns true on success, false on failure.
my $pid = $pidfile->running; die "Service already running: $pid" 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 undef is returned.
undef
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, undef is returned.
See Changes file
Changes
See TODO file
TODO
* none noted
File::Pid
Serż Minus (Sergey Lepenkov) https://www.serzik.com <abalama@cpan.org>
Copyright (C) 1998-2022 D&D Corporation. All Rights Reserved
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See LICENSE file and https://dev.perl.org/licenses/
LICENSE
To install CTK, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CTK
CPAN shell
perl -MCPAN -e shell install CTK
For more information on module installation, please visit the detailed CPAN module installation guide.