#!/usr/bin/perl # lock-self.pl - how to lock the script itself use 5.36.0; use Fcntl ':flock'; eval { local $SIG{ALRM} = sub { die "timeout\n" }; alarm 1; say "$$\ttry lock"; flock *DATA, LOCK_EX; alarm 0; 1; } or do { die unless $@ eq "timeout\n"; say "$$\tno lock"; exit 0; # meh }; say "$$\tgot lock"; sleep 3; say "$$\trelease lock"; __DATA__ this is for the lock, do not remove