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

Hello, When multiple scripts trigger ( same script with different parameters). The first script gets access to a status file and others should wait in the line. Once the script completes running. the second one should get access to the file. I am new to perl scripting so any pointers will help. Thank you
  • Comment on script should wait for another script to complete

Replies are listed 'Best First'.
Re: script should wait for another script to complete
by Corion (Patriarch) on Aug 22, 2017 at 14:39 UTC
Re: script should wait for another script to complete
by thanos1983 (Parson) on Aug 22, 2017 at 16:15 UTC

    Hello skatpally,

    Welcome to the Monastery. There is a recent "very similar" question with some nice suggestions in order to achieve what you need perl script and queue.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      Thank you every one reply. what I am looking is when the file is locked and multiple Perl commands are triggered. I want to know an approach where each of the commands is in the queue and each gets triggered one after another.

        Why not a loop rather than a queue ?

        #!perl use strict; my @param = ( [1,2],['a','b'],['x','y','z'] ); for (@param){ local @ARGV = @$_; do "myscript.pl"; }
        #!perl # myscript.pl use strict; print "Running myscript.pl with @ARGV \n"; sleep 2; print "Finished\n";
        poj

        Hello again skatpally,

        Sorry for the late reply but I got busy with something else.

        Regarding your question what I am looking is when the file is locked and multiple Perl commands are triggered. I want to know an approach where each of the commands is in the queue and each gets triggered one after another. I think the best solution to your problem is Thread::Queue.

        More specifically you should read the Thread::Queue/ADVANCED METHODS. There you will find ways in checking/adding/altering the queue of you commands in a way you desire.

        The link that I provided you contains an example of this module Re^3: perl script and queue. Any way, a more complete code example that could give you a close solution to what you desire is provided bellow.

        Sample of executable code:

        #!/usr/bin/perl use strict; use threads; use warnings; use feature 'say'; use Thread::Queue; sub executeCommands { my ($commands) = @_; say $commands; } my $q = Thread::Queue->new(); # A new empty queue # Set a size for a queue $q->limit = 5; # Worker thread my $thr = threads->create( sub { # Thread will loop until no more work while (defined(my $item = $q->dequeue())) { # Do work on $item executeCommands($item); } } ); my @commands = ("Command_1", "Command_2"); # Send work to the thread $q->enqueue(@commands); { lock($q); # Keep other threads from changing the queue's content +s my @secondary_commands = ("Command_3", "Command_4"); # Insert two items into the queue in the head $q->insert(-2, @secondary_commands); my $item = $q->peek(); # do something with the items in queue if desired if ($item eq "Command_3") { my @third_commands = ("Command_5", "Command_6"); # Insert two items into the queue just behind the peek $q->enqueue(@third_commands); } } # Queue is now unlocked # Signal that there is no more work to be sent $q->end(); # Join up with the thread when it finishes $thr->join(); __END__ $ perl test.pl Command_3 Command_4 Command_1 Command_2 Command_5 Command_6

        Hope this helps, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: script should wait for another script to complete
by Anonymous Monk on Aug 24, 2017 at 18:48 UTC
    use Fcntl ':flock'; # Import LOCK_* constants # We will use this file path in error messages and function calls. # Don't type it out more than once in your code. Use a variable. my $file = '/path/to/some/file'; # Open the file for appending. Note the file path is quoted # in the error message. This helps debug situations where you # have a stray space at the start or end of the path. open(my $fh, '>>', $file) or die "Could not open '$file' - $!"; # Get exclusive lock (will block until it does) flock($fh, LOCK_EX) or die "Could not lock '$file' - $!"; # Do something with the file here... # Do NOT use flock() to unlock the file if you wrote to the # file in the "do something" section above. This could create # a race condition. The close() call below will unlock the # file for you, but only after writing any buffered data. # In a world of buffered i/o, some or all of your data may not # be written until close() completes. Always, always, ALWAYS # check the return value of close() if you wrote to the file! close($fh) or die "Could not write '$file' - $!";