💾 Archived View for thrig.me › blog › 2023 › 07 › 24 › lock-self.pl captured on 2023-09-08 at 17:10:20.

View Raw

More Information

-=-=-=-=-=-=-

#!/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