Perl: File Locking

Published by

Posted on August 18, 2016

A good function to use to force one instance of a script to run. Flock

Code snippet

[perl]

use Fcntl ":flock"; # allow only one instance to run
open(my $self, "<", $0) ||
die("001 : Cannot open myself : $!.\n");
flock($self, LOCK_EX|LOCK_NB) ||
die("002 : Another user is running the tool.\n ");

[/perl]