in reply to Scheduling Perl Tasks

To prevent multiple instances of the same script from running, I found it very beneficial to (try to) lock $0.

This approach will fail for symlinks and hardlinks maybe.

Replies are listed 'Best First'.
Re^2: Scheduling Perl Tasks
by marioroy (Prior) on Mar 23, 2017 at 23:24 UTC

    That's a good point Corion regarding locking $0. I updated the code by not appending the suffix ".lock" when path is given. The lock is released automatically upon termination of the script.

    use Mutex::Flock; ( my $mutex = Mutex::Flock->new( path => $0 ) )->lock_exclusive; ...

    Another way, with the timewait method.

    my $mutex = MCE::Mutex::Flock->new( path => $0 ); # terminate script if a previous instance is still running exit unless $mutex->timedwait(2); ...

    The module is beneficial regarding supporting threads and processes. I will make time to complete it and subsequently a release for CPAN.

    Regards, Mario

    Edit: Added timedwait example.