package My::ThereCanBeOnlyOne; use strict; use LockFile::Simple; my $Lock; sub import { my $self = shift; my $name = shift || 'therecanbeonlyone'; my %args = @_; # Using /tmp allows an internal denial of service attack. my $lockfile = "/tmp/$name.pid"; 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 = ; close(LOCKFILE); chomp($other_pid); die __PACKAGE__.": $name: $other_pid still running. $$ exiting\n"; } END { $Lock->release(); } 1; #### use My::ThereCanBeOnlyOne 'myprogram'; #### use My::ThereCanBeOnlyOne 'myprogram', -hold => 0, -stale => 1, -max => 1; #### use My::ThereCanBeOnlyOne 'myprogram'.int(rand(6));