File::TinyLock - Utility for process locking and unlocking.
use File::TinyLock; my $LOCK = '/tmp/testing.lock'; my $locksmith = File::TinyLock->new(lock => $LOCK); if( $locksmith->lock() ){ warn "we have locked\n"; $locksmith->unlock(); warn "we have unlocked\n"; }else{ warn "could not lock..\n"; }
File::TinyLock provides lock, unlock, and checklock methods for working with process locking. This utility attempts to be useful when you require one of a process to be running at a time, but someone could possibly try to spawn off a second (such as having a crontab where you are *hoping* one job ends before the next starts).
File::TinyLock
lock
unlock
checklock
LOCK is a mandatory lock file.
LOCK
OPTIONS are passed in a hash like fashion, using key and value pairs. Possible options are:
OPTIONS
mylock - Unique file to identify our process (Default: auto-generated) - *must* be on the same filesystem as <LOCK>
retries - Number of times to retry getting a lock (Default: 5)
retrydelay - Number of seconds to wait between retries (Default: 60)
debug - Print debugging info to STDERR (0=Off, 1=On) (Default: 0).
Here are a list of return codes of the lock function and what they mean:
.. and for the checklock function:
.. and the unlock function:
# run the below code twice ( e.g. perl ./test.pl & ; perl ./test.pl ) use strict; use File::TinyLock; my $lock = '/tmp/testing.lock'; my $locksmith = File::TinyLock->new(lock => $lock, debug => 1); my $result = $locksmith->lock(); if($result){ print "We have obtained a lock\n"; } # do stuff sleep 30; $locksmith->unlock(); exit;
If you leave lock files around (from not unlocking the file before your code exits), File::TinyLock will try its best to clean up and/or determine if the lock files are stale or not. This is best effort, and may yield false positives. For example, if your code was running as pid 1234 and crashed without unlocking, stale detection may fail if there is a new process running with pid 1234.
Locking will only remain successfull while your code is active. You can not lock, let your code exit, and start your code again - doing so will result in stale lock files left behind.
start code -> get lock -> do stuff -> unlock -> exit;
<a href="http://jeremy.kister.net./">Jeremy Kister</a>
3 POD Errors
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
'=item' outside of any '=over'
To install File::TinyLock, copy and paste the appropriate command in to your terminal.
cpanm
cpanm File::TinyLock
CPAN shell
perl -MCPAN -e shell install File::TinyLock
For more information on module installation, please visit the detailed CPAN module installation guide.