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

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 &"); system("perl"); #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 (Canon) 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"); system "perl"); ... my $result = waitpid($child, 0); }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2021-12-07 22:45 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (34 votes). Check out past polls.