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

Hi.

I have a perl script that uses open() to run another perl script, and and then print some data to it.

Is there any way to know if the opened process exits normally or dies with any error? (maybe using some other method.. open() is the easiest way.. :) I need the original process to exit with the same error as the opened process.. Is that possible?

Thanks.

Replies are listed 'Best First'.
Re: knowing if process dies.
by tye (Sage) on Aug 24, 2000 at 18:35 UTC

    close on a pipe to another process will return a false value if the process did not exit normally. In that case, either $! will be set to some internal error that prevented you from running the process or $? will be set to the exit status of the process. See the documentation for wait() for information on how to extract what exit status to use from $?.

    The status you get from a child says:

    • Did the process die from a signal?
    • If so what signal and did it dump core?
    • If not, what number did it exit with?
    If the process just exited with a non-zero value, then you can exit with the same value. If the process died from a signal, then you could try killing yourself with a signal and hope you also die and hope that you either core dump or don't just like the other process.

    If the other process was run from the shell, then you get the exit status of the shell. This usually (varies by shell) tries to summarize the exit status of the child by using special exit values to indicate death by signal. But you can easily lose some information in the translation.

            - tye (but my friends call me "Tye")
Re (tilly) 1: knowing if process dies.
by tilly (Archbishop) on Aug 24, 2000 at 16:52 UTC
    OK, just checked perldoc -f open and found that if you open a pipe from a process, your return code is the pid of the process. Memorize that, read until you can read no longer, then call wait. Check that you reaped the right process (if you only run one then you darned well should!) and then you have its return code to exit with.
Re: knowing if process dies.
by xdb19 (Sexton) on Aug 24, 2000 at 18:51 UTC
    Tye was very helpful in helping me find out how to do it. I now have actual code to get the error code ( tested, and working ) .. I first created a script that would exit out with an error. THis was simple.

    print "XDB19\n"; exit( 3 );


    Now I needed a program to call this one, and get it's output and error code.

    open( LOG, "perl ex.pl|" ); # open the program while ( <LOG> ) { print "$_"; } # print output from prog close( LOG ); # wont work without close $errcode = ( $? / 256 ); # there is a number stored # in the $? variable, and # it happens to be the exit # code * 256 ...so divide, # and conquer print "***$errcode***\n"; #print it ( stands out doesnt it )
    Well, thats it. I cant vote yet Tye, but you should be voted up for your reply. It led me to this code. I hope this helps.

    - Have fun, XDB19
      Hmm. Dividing by 256 isn't quite right, because you could get a fractional number. Maybe this is closer:
      my $errcode = $? >> 8; my $core_was_dumped = !!($? & 128); my $signal_that_killed_it = $? & 127;
      This is from memory, so if I messed up, I'm sure someone will correct me. {grin}

      -- Randal L. Schwartz, Perl hacker

Re: knowing if process dies.
by Punto (Scribe) on Aug 24, 2000 at 18:39 UTC
    (ok, replying to myself :)
    After closing the filehandle, the status value is returned on $?. It returns "65280" when I use die, and 0 when the program exits normally.

    Sorry to bother you people.. :-)