NAME

Acrux::FilePid - The Pid File simple interface

SYNOPSIS

use Acrux::FilePid;

my $fp = Acrux::FilePid->new (
    file => '/some/file.pid',
);

if ( my $num = $fp->running ) {
    die "Already running: $num";
} else {
    $fp->save;
    # . . .
    $fp->remove;
}

... or with autoremove:

my $fp = Acrux::FilePid->new (
    file => '/some/file.pid',
    autoremove => 1,
);
die "Already running" if $fp->running;
$fp->save;
# . . .

... or with autosave and autoremove

my $fp = Acrux::FilePid->new (
    file => '/some/file.pid',
    auto => 1,
);
die "Already running" if $fp->running;
# . . .

DESCRIPTION

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.

new

my $fp = Acrux::FilePid->new;

my $fp = Acrux::FilePid->new(
    file => '/var/run/daemon.pid',
);

my $fp = Acrux::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 current directory. So, for example, if $0 is ~/bin/sig.pl, the pid file will be ./sig.pl.pid.

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.

autoremove - Auto-remove flag. If this flag specified as true, then will be removed the pid file automatically on DESTROY phase. Default: false

autosave - Auto-save flag. If this flag specified as true, then will be saved the pid file automatically while instance create. Default: false

auto - this flag forced sets autoremove and autosave flags. Default: false

file

$fp->file("/var/run/file.pid");
my $pidfile = $fp->file;

Accessor/mutator for the filename used as the pid file.

load

$fp->load;

Load owner pid from file. On success, the object is returned. On failure, undef is returned.

owner

$fp->owner(123);
my $owner = $fp->owner;

Accessor/mutator for the pid being saved to the pid file.

pid

$fp->pid(123);
my $pid = $fp->pid;

Accessor/mutator for the pid being saved to the pid file.

remove

$fp->remove;

Removes the pid file from disk. Returns true on success, false on failure.

running

my $pid = $fp->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.

save

$fp->save;

Writes the pid file to disk, inserting the pid inside the file. On success, the object is returned. On failure, undef is returned.

HISTORY

See Changes file

TO DO

See TODO file

SEE ALSO

CTK::FilePid, Acme::Ghost::FilePid

AUTHOR

Serż Minus (Sergey Lepenkov) https://www.serzik.com <abalama@cpan.org>

COPYRIGHT

Copyright (C) 1998-2024 D&D Corporation. All Rights Reserved

LICENSE

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/