I tried this:
#!/usr/bin/perl
use strict;
use warnings;
$|=1;
use Sys::RunAlone;
print "Script $0 is running now.\n";
die "Another $0 is already running.\n"
unless open my $self, '<', $0;
CheckRaceCondition();
sleep(5);
print "End.\n";
sub CheckRaceCondition {
die "Another $0 is already running.\n"
unless open $self, '<', $0;
}
__END__
For an in-depth discussion of Fcntl and flock see:
Ensuring only one copy of a perl script is running.
Then, for a great solution to the problem by
ikegami, see:
Re^2: Ensuring only one copy of a perl script is running