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

I am using Parallel::ForkManage. While running run_on_finish, I am checking what is the return code of the Child Process. Now, in child process i am running curl to download a file. While downloading some times the remote server goes down and curl terminates in between. Now, when this happens and I check the return exit code in run_on_finish, it is coming as zero while when i reproduce this same thing on shell prompt, it returns me error code ($? to be 175 or a non zero). Can you help me trap correct error code in run_on_finish? Here is my code:
$pm->run_on_finish ( sub { my ($pid, $exit_code, $ident, $signal, $core) = @_; if ($core) { #If there is a core, report it print "Process $pid dumped core\n"; } else { #Collecting process info in Hash to be reported at the end + of script. #print "Process $pid executed successfully\n"; } $pidInfoHash{$pid} = $exit_code; } ); for (1..$total_calls/$cps) { for (1..$cps) { my $pid = $pm->start and next; RunChild(); $pm->finish; } sleep(1); } sub RunChild { if (defined $proxy) { $ENV{http_proxy}="$proxy"; } $logFile = $logDir."/header_".$call; `curl -o /dev/null -m 222 \"$Link\" 2>> $logFile`; my $curlResult = `echo $?`; chomp($curlResult); if ($curlResult != 0) { print "Curl Result $curlResult :: Call Drop : $callDrop\n"; $callDrop++; print "Curl Result $curlResult :: Call Drop : $callDrop\n"; } }
  • Comment on Parallel::ForkManager run_on_finish Canot get Exit Code of Child process
  • Download Code

Replies are listed 'Best First'.
Re: Parallel::ForkManager run_on_finish Canot get Exit Code of Child process
by moritz (Cardinal) on Dec 02, 2011 at 09:40 UTC
    `curl -o /dev/null -m 222 \"$Link\" 2>> $logFile`; my $curlResult = `echo $?`;

    That code is quite convoluted.

    Each `...` call starts a separate shell, so the second shell doesn't know the return value of a program started in the first shell. And even if it would, you'd not ask it for the return value of the previously launched program, because you interpolate the perl variable $? -- which you could use directly anyway.

    But there are more robust ways to interface curl anyway, for example WWW::Curl and LWP::Curl.

Re: Parallel::ForkManager run_on_finish Canot get Exit Code of Child process
by ikegami (Patriarch) on Dec 02, 2011 at 09:04 UTC

    Your child doesn't pass an exit code to finish or call exit to set its exit code to anything but zero.

      No Success. I added exit() in my child process, but nothing was returned by run_on_exit as exit code. Please check that.

        I don't believe that run_on_exit didn't provide the exit code. Maybe you meant it was zero?

        If the exit code is zero, then you're still not setting it to something other than zero. It's hard to tell what you're doing wrong if you don't show me what you did. What value did you pass to exit?

Re: Parallel::ForkManager run_on_finish Canot get Exit Code of Child process
by runrig (Abbot) on Dec 02, 2011 at 16:49 UTC
    Instead of:
    `curl -o /dev/null -m 222 \"$Link\" 2>> $logFile`
    Try:
    open(STDERR, ">>", $logfile) or die "Failed to open $logfile: $!"; exec curl => -o => "/dev/null", -m => 222, $Link; die "Could not exec curl: $!";
    Now the run_on_finish() should capture the exit status of curl (or some other failure like opening the log file or not being able to run curl).