The following script is an example of using lock files in Perl. This prevents more than one user or process accessing a file at a time.

use Fcntl qw(:DEFAULT :flock);
use Fcntl ‘:flock’;¬†¬†¬†¬† #LOCK_* constants


unless (createlock()) {
print “Lock has been created”;
print “This is only run if their was no lock file present”;


sub releaselock
# Only remove the lockfile if we created it.
if ( $lockneedsremoving  )
writelog (“Removing sudo lock”);
-f “${lockfilename}” and unlink “${lockfilename}”;

undef $lockneedsremoving;

sub createlock
sysopen(LOCKFILE, “${lockfilename}”, O_RDWR|O_CREAT) or print (“Cannot open lockfile ${lockfilename}: $!”);
unless (flock(LOCKFILE, LOCK_EX|LOCK_NB))
print “Another update is in place.¬† Please try in a minute…”;
return 0;

return 1;