Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

how to detect background process completed or not

by kamesh3183 (Beadle)
on Aug 17, 2005 at 06:34 UTC ( #484322=perlquestion: print w/replies, xml ) Need Help??

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

I have following perl code
for (i=0; i<$counter; i++) { system("perl test1.pl &"); system("perl test2.pl"); #I want to continue with the next iteration when I am sure that backgr +ound process is completed #How can I detect above condition? }
How can I detect whether background process is completed or not when using perl system function?

Replies are listed 'Best First'.
Re: how to detect background process completed or not
by Tanalis (Curate) on Aug 17, 2005 at 06:59 UTC
    Using the CPAN module Proc::Background, you should be able to write something like ..
    use Proc::Background; my $proc = Proc::Background->new( $cmd ); $proc->wait; # wait for completion
    Maybe a case of cracking a nut with a sledgehammer for this example, but it'd seem nice and clean, and to do what you need.

    Hope that helps ..

Re: how to detect background process completed or not
by salva (Abbot) on Aug 17, 2005 at 07:05 UTC
    fork the new process on the perl side, instead of on the shell:
    sub system_background { my $pid = fork; if(defined($pid) and $pid==0) { exec(@_); exit(1) } $pid } for (...) { my $child = system_background("perl test1.pl"); system "perl test2.pl"); ... my $result = waitpid($child, 0); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://484322]
Approved by lamp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2020-11-29 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?