Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

delay loop

by Vasilis (Initiate)
on Apr 04, 2014 at 10:56 UTC ( #1081108=perlquestion: print w/replies, xml ) Need Help??
Vasilis has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks, I'm a new Perl user, and probably my problem seems to be stupid, but I really need your help.

I made a pipeline in order to send several jobs in an lsf based cluster. After sending my jobs, I want to make a delay loop in order to wait the script till the jobs are finished, and continue after that.

In bash scripting I would use something like that:

while [ 1 ] ; do cmd=`bjobs 2> /dev/null` if [[ $cmd =~ "name_of_jobs" ]]; then #echo "jobs still in queue..." sleep 20; else break fi done

unfortunately I don't know how can I control the name of the jobs by using Perl.

Thank you very much in advance,


Replies are listed 'Best First'.
Re: delay loop
by RMGir (Prior) on Apr 04, 2014 at 11:47 UTC
    Hi Vasilis!

    I think you want to look at waitpid, and look around the monastery for examples of using it.

    There are also many CPAN modules that wrap forking off processes, so a visit to may be productive.

    (Edit: Introduction to Parallel::ForkManager looks like a good place to start...)

Re: delay loop
by zentara (Archbishop) on Apr 04, 2014 at 20:19 UTC
    I didn't write this, but it does seem to answer your needs. Another option is to actually use an eventloop system like Glib or AnyEvent.
    #!/usr/bin/perl use warnings; use strict; # by some monk here at perlmonks # wait basically waits until one of the children exits. # It is used to avoid having Zombie processes: if you don't # wait() for them, and you go a long time in your parent # process before exiting, you'll see these processes in # the process list even if they already exited, marked as # "zombie" (this means that resources in the kernel associated # to the process aren't freed). So, wait is the way by which # the parent process acknowledges the kernel of the son's death. # It's interesting that this mechanism actually tries to force # the programmer not to ignore return values from child processes, # much like the good practice of never throwing away return values # from functions. # Moreover, wait returns as soon as the first child exits; in # the OP's post, (s)he created 8 of them, and had to wait until # all of them have exited. According to perldoc -f wait, a call # to the wait function returns -1 if, and only if, there are no # more children alive(1) - thus the cycle. # If you happened to set $SIG{'CHLD'} to 'IGNORE', # this zombie-ridding happens automatically, and you're likely to # have wait always return -1 - but I guess it's not the case with # this thread :) Moreover, in this case I would discourage the # OP to do that, because (s)he's using wait as a synchronisation # mechanism to understand when all the children exited. BTW, this # is documented in perldoc perlipc, but I suggest taking a look in # perldoc perlport as well. for ( my $i = 0 ; $i < 11 ; $i++ ) { defined( my $pid = fork() ) or print STDERR "Unable to fork!"; if ($pid) { print "Child $i is $pid\n"; } else { print "Hi! I'm just a kid.. I'm going to sleep now..\n"; sleep rand(10); print "*Yawn* I'm awake! Huh? It's me! Kid $$! Bye bye! $$ Time to die!\n"; exit 0; } } #this is correct 1 while wait > 0; # wait by itself is different # wait; print "Screw this, I'm done waiting for those kids\n";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: delay loop
by monarch (Priest) on Apr 04, 2014 at 14:55 UTC
    Some operating systems will respect Perl altering the job name through manipulation of the $0 variable (that's dollar-zero). E.g.
    $0 = "SpeciallyNamedJob"; sleep 60;

    or a one-liner: perl -e '$0="SpeciallyNamedJob"; sleep 60'

    If you type: ps ax you'll see:

    3545971 pts/13 S+ 0:00 SpeciallyNamedJob

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1081108]
Approved by Corion
[Discipulus]: still happy Happy-the-monk? even in 2018? good for you.. ;=)
[choroba]: Good morning!

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2018-01-23 08:46 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (242 votes). Check out past polls.