9mohit2 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I am planning to replace a automation tool with Perl scripts. So I have to schedule Perl scripts to run. There are two types of task one which are triggered via input mail another are to be run on a particular time and day of the week.

I am planning to make a .bat file which calls the respective script to be scheduled using Windows Task Scheduler. Wanted suggestions from you guys as will it be a good idea can there be complexities like:

- Scripts running in parallel can cause error by reading and writing from each other excels.
- Will the scripts run even if the system is locked? It runs but Win32::OLE scripts stuck sometimes.

Your suggestions will be great for me. Thanks.

Replies are listed 'Best First'.
Re: Scheduling Perl Tasks
by davido (Cardinal) on Mar 23, 2017 at 06:08 UTC

    File locking is hard to do right but possible, and can be used to ensure your process doesn't collide with another instance of itself.

    There will certainly be caveats under Windows, some of which might be enumerated in perlport.


    Dave

      I think there is some lock modules on cpan that simplify/abstract the complexity
      Hi Dave,

      What if I can manage that same script does not runs in parallel will perl scritps accessing separate excel files can also cause any issue?? I also used Win32::OLE module for many scripts.

        So you now are ok with the same script running as long as it's acting on different files? Then yes, that's just fine. Use flock, as discussed in the slides I linked to, to lock the files you are acting upon. That will ensure that multiple instances of your script cannot possibly touch the same file. If you do that, sure, there's nothing wrong with running the same script multiple times simultaneously against different data sets, so long as none of the instances attempt to write to the same file.


        Dave

Re: Scheduling Perl Tasks
by Corion (Patriarch) on Mar 23, 2017 at 08:15 UTC

    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.

      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.

Re: Scheduling Perl Tasks
by marioroy (Prior) on Mar 23, 2017 at 07:08 UTC

    Greetings 9mohit2,

    I put together a Fcntl-based (flock) advisory locking module posted here. Any Perl toolbox might want this.

    I'm sorry, but lack the time making modules for CPAN. This is my contribution to the Perl community. Thanks all. I love PerlMonks and enjoyed posting solutions from time to time.

    Regards, Mario

      Greetings.

      The project is completed including a release on CPAN. To ensure a given script is executing once, the following is how to do it using Mutex. That will block until obtaining an exclusive lock. A false value is returned if the timeout is reached, and a true value otherwise.

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

      Regards, Mario

      Edit: Updated the CPAN link to point to the 1.001 release.

Re: Scheduling Perl Tasks
by poj (Abbot) on Mar 23, 2017 at 19:08 UTC
    Scripts running in parallel can cause error

    Have you considered using a job queue something like Queue::DBI perhaps ?.

    poj