NAME

PID::File - PID files that guard against exceptions.

VERSION

Version 0.14

SYNOPSIS

Create PID files.

use PID::File;

my $pid_file = PID::File->new;

exit if $pid_file->running;

if ( $pid_file->create )
{
    # do something
     
    $pid_file->remove;
}

Or perhaps a bit more robust...

while ( $pid_file->running || ! $pid_file->create )
{
    print "Already running, sleeping for 2\n";
    sleep 2;
}

my $guard = $pid_file->guard;

# if we get an exception at this point, the guard will go out
# of scope, which calls $pid_file->remove() automatically

$pid_file->remove;

DESCRIPTION

Creating a pid file, or lock file, should be such a simple process.

See Daemon::Control for a more complete solution for creating daemons (and pid files).

The code for this module was largely borrowed from there.

Methods

Class Methods

new

my $pid_file = PID::File->new;

Instance Methods

file

The filename for the pid file.

$pid_file->file( '/tmp/myapp.pid' );

If you specify a relative path, it will be relative to where your scripts runs.

By default it will use the script name and append .pid to it.

create

Attempt to create a new pid file.

if ( $pid_file->create )

Returns true or false for whether the pid file was created.

If the file already exists, and the pid in that file is still running, no action will be taken and it will return false.

running

if ( $pid_file->running )

Returns true or false to indicate whether the pid in the current pid file is running.

remove

Removes the pid file.

$pid_file->remove;

guard

Returns a token that will call remove when it goes out of scope.

This deals with scenarios where your script may throw an exception before being able to remove the lock file.

You b<must> assign the return value of guard to some token.

if ( $pid_file->create )
{
    my $guard = $pid_file->guard;

    # do something that could possibly die
    # before being able to call $pid_file->remove
    
    $pid_file->remove;
}

AUTHOR

Rob Brown, <rob at intelcompute.com>

BUGS

Please report any bugs or feature requests to bug-pid-file at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PID-File. I will be notified, and then you will automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc PID::File

You can also look for information at:

SEE ALSO

Daemon::Control

Scope::Guard

LICENSE AND COPYRIGHT

Copyright 2012 Rob Brown.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.