Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

How to Trap exit code from system calls

by emjga1 (Novice)
on Jan 21, 2004 at 15:56 UTC ( #322913=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks

The following snipet of code , works fine
But how can I trap the exit code of the program check_nrpe
Which could be 0 , 1 , 2 or 254

open(CMD, "/local/nagios/libexec/check_nrpe -H $host|") or dir $!; @CMD=<CMD>; close(CMD);
I think I should be using fork or waitpid
but not sure how ?.



Replies are listed 'Best First'.
Re: How to Trap exit code from system calls
by Abigail-II (Bishop) on Jan 21, 2004 at 16:10 UTC
    How about RTFM? From perldoc -f close:
    If the file handle came from a piped open "close" will additionally return false if one of the other system calls involved fails or if the program exits with non-zero status. (If the only problem was that the program exited non-zero $! will be set to 0.) Closing a pipe also waits for the pro- cess executing on the pipe to complete, in case you want to look at the output of the pipe after- wards, and implicitly puts the exit status value of that command into $?.


Re: How to Trap exit code from system calls
by Fletch (Chancellor) on Jan 21, 2004 at 16:12 UTC

    If you read perldoc -f open carefully:

    Closing any piped filehandle causes the parent process to wait for the child to finish, and returns the status value in $?.
Re: How to Trap exit code from system calls
by tcf22 (Priest) on Jan 21, 2004 at 15:59 UTC
    You could always just use a system call. Below is what the docs say about checking exit values and such.
    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;

    - Tom


      Thanks , I had looked at $? but did not get the expected answers from it
      and asumed I was doing somthing wrong.

      But using
      $exit_value  = $? >> 8;

      Does just the job.



Re: How to Trap exit code from system calls
by geektron (Curate) on Jan 21, 2004 at 16:00 UTC
     system() will return zero on success or nonzero on failure.

    you can examine the return value in $?.

    perldoc -f system is your friend. :-)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://322913]
Approved by tcf22
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2021-12-09 14:59 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (36 votes). Check out past polls.