The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

CTK::FilePid - The Pid File simple interface

VERSION

Version 1.05

SYNOPSIS

    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;

        # ...
    }

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.

This module is based on File::Pid module

new

    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.

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

file

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

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

pid

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

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

remove

    $pidfile->remove or warn "Couldn't unlink pid file";

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

running

    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.

write

    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.

HISTORY

See Changes file

TO DO

See TODO file

BUGS

* none noted

SEE ALSO

File::Pid

AUTHOR

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

COPYRIGHT

Copyright (C) 1998-2022 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/