This is Bavaria, Germany!
#!/usr/bin/perl
use strict;
use warnings;
use Fcntl qw/LOCK_EX LOCK_NB/;
use File::Basename;
my ( $lock_fh, $lock_fqn );
# Lockfile with the same name as the program itself
$lock_fqn = "/var/lock/" . basename($0);
open( $lock_fh, ">", $lock_fqn ) || die "Could not open $!";
unless ( flock( $lock_fh, LOCK_EX | LOCK_NB ) ) {
die "Can't lock with file $lock_fqn, another process is locking";
}
# do something which should be done only once
# on this system
foreach ( 1 .. 10 ) {
print "Wait $_/10\n";
sleep 5;
}
# when the programm terminates, don't
# forget to close the lockfile
print "Closing logfile $lock_fqn\n";
close $lock_fh;
unlink $lock_fqn;