Link Search Menu Expand Document

Perl lock file

without touching a file

If you have any scripts in your crontab that are run regularly but sometimes run for too long and end up running twice you need something like a lock file/pid file. This can get messy if scripts crash, servers reboot, etc and the lock file doesn’t get remove and - oh no - the script hasn’t been running all day…

Another solution to locking a file from being executed more than once in perl is like this.


use Fcntl ':flock'; # import LOCK_* constants

  open  *{0}
        or die "What!? $0:$!";
  flock *{0}, LOCK_EX|LOCK_NB
    or die "$0 is already running somewhere!n";