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

I have a perl script that starts several child processes (via 'fork') and each of these child
processes performs a 'system' command to start another perl script.

I need to know how to detect if an error has occured within any of the 'children' perl scripts
(the errors are handled by 'die' statements).

The problem is that the 'system' command returns a value of -1 when the final child process completes,
regardless of whether an error occured.

I've checked the value of the $? variable and it is also set to -1 when the 'system' command for the
final child process returns.

How can I know if the final child process terminated normally or had errors?

Thanks.

Replies are listed 'Best First'.
Re: Capturing Errors from Forked Processes
by Fastolfe (Vicar) on Nov 08, 2000 at 21:16 UTC
    The result of a system call should be 0 if the command completed successfully. Otherwise, you need to examine (as you were doing) the $? variable to get the exit status of the script. See the documentation for system for information on how to make sense of this number (it does not immediately equal the exit status of the child process).

    If you're trying to get the exit status of your child processes (after you fork), you need to examine $? after your wait or waitpid calls (when you reap your child processes).

    If you're consistently getting -1 with system and $?, regardless of whether or not there was an error, I would say that's odd and that something is not as you say it is. Test your assumptions again.