You can also specify LockFile::Simple options to change the behavior as in:use My::ThereCanBeOnlyOne '/tmp/myprogram.pid';
It is recommended you chose a lockfile outside of /tmp as that would allow a denial of service attack from another user on the same machine.use My::ThereCanBeOnlyOne '/tmp/myprogram.pid', -hold => 0, -stale => 1, -max => 1;
package My::ThereCanBeOnlyOne; use strict; use LockFile::Simple; my $Lock; sub import { my $self = shift; my $lockfile = shift || die "please provide lockfile"; my %args = @_; my $locker = LockFile::Simple->make(-autoclean => 0, %args); $Lock = $locker->lock($lockfile, '%f') and return 1; open(LOCKFILE, "< $lockfile") or die __PACKAGE__."Unable to open $lockfile: $!"; my $other_pid = <LOCKFILE>; close(LOCKFILE); chomp($other_pid); die __PACKAGE__.": $lockfile: $other_pid still running. $$ exiting\n +"; } END { $Lock->release(); } 1;
In reply to Only let one copy of program run at a time by esh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |