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