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

I'm trying to capture the exit status of a spawned process. The script conf.pl executes and I inserted a fault, so when all is well it returns 0 and with fault it returns 13
$? returns 0 even it there is a fault. Any thoughts?
use Run; my $cfg_test = "conf_ini"; my $cfg_stat = "fresh"; my $system_call = '/test/conf.pl'; $pid = spawn $system_call, $cfg_test, $cfg_stat or die "spawn: $!"; push @pids, $pid; my $num_proc1 = @pids; while ($num_proc1) { for ( my $i=0; $i< @pids; $i++) { waitpid($pids[$i],0); $num_proc1 --; $pid --; } print "DOES THIS -- $? -- CAPTURE ANYTHING\n"; if ( $system_call >> 8, 0, 'Normal exit' ){ print "good\n";} if( $exit >> 8, 42, 'Non-zero exit' ){ print "bad\n";} if ($? == 13) { print "failed: $!\n"; } elsif ($? & 127) { print "$pid died with signal $?\n", ($? & 127), ($? & 128) ? 'with' : 'without'; } elsif ($? > 0) { print "$pid died with signal $?\n"; } else { print "$pid exited with value $?\n", $? >> 8; }

Replies are listed 'Best First'.
Re: Spawned process exit status
by VSarkiss (Monsignor) on Jun 22, 2005 at 15:49 UTC
      I'm running RH9. Perl version is 5.8.0
Re: Spawned process exit status
by waswas-fng (Curate) on Jun 22, 2005 at 16:45 UTC
    Run and spawn exec a process in background mode. It only returns error codes for the exec return of the process -- not the result exit code. Use system() to get the exit code.
    from the perldoc for system:
    Because system() and backticks block SIGINT and SIGQUIT, killing the p +rogram they're running doesn't actually interrupt your program. @args = ("command", "arg1", "arg2"); system(@args) == 0 or die "system @args failed: $?" You can check all the failure possibilities by inspecting $? like this +: $exit_value = $? >> 8; $signal_num = $? & 127; $dumped_core = $? & 128;


    -Waswas
      I made the changes and still I get the same. The script exits with an error code of 13 and $? reports back with 0.
      @args = ("/test/conf.pl", $cfg_test, $cfg_stat ); system(@args) == 0 or die "system @args failed: $?"; $exit_value = $? >> 8; $signal_num = $? & 127; $dumped_core = $? & 128; print "EXIT VALUE = $exit_value\n"; print "SIGNAL = $signal_num\n"; print "CORE = $dumped_core\n";
        That is because it never gets to your print "EXIT VALUE.. line when there is a non 0 exit on the conf.pl script. You explicitly tell it to die and spit out the unprocessed error held in $?.
        What do you think this line does? system(@args) == 0 or die "system @args failed: $?";


        -Waswas