The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Local::LockFile::PID - pid lock with an advisory file

SYNPOSIS

use Local::LockFile::PID;

my $file = '/lock/file/path';
my $lock = Local::LockFile::PID->new( $file );
my $pid = $lock->lock();

if ( $pid )
{
    if ( my $child = fork() )
    {
        my $handle = $lock->handle();
        syswrite $handle, $child;
        exit 0;
    }

    ## child safely does critical stuff
}
else
{
    my $pid = Local::LockFile::PID->check( $file );

    die "another instance $pid already running\n" if $pid;
}

DESCRIPTION

lock()

Attempts to acquire lock. Returns pid if successful. Returns undef otherwise.

handle()

Returns the handle of the lock file.

check( $filename )

Returns ID of process that owns the lock. Returns 0 if not locked.

SEE ALSO

Fcntl

NOTE

See Local::Util