Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

system() status weirdness

by monk2b (Pilgrim)
on Mar 28, 2008 at 16:57 UTC ( #677054=perlquestion: print w/replies, xml ) Need Help??

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

I like to test the error status of system() when I use that function. Recently I had reason to use the module Net::SSH:Expect in a program of mine. I have noticed that the value of $? after a system() call has changed to -1.

If I comment out the line "use Net:SSH:Expect" I get the results I am use to seeing a 0 for no error and 256 for error. If I uncomment "use Net::SSH:Expect" the value of $? is -1 on error or success. I am not sure what the real issue is here. Any info would be appreciated.

This is a simplistic code example of what is happening.

Bob
#!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; my $command0 = "ls -l /tmp"; my $command1 = "ls -l /temperature"; RunCmd(); sub RunCmd{ system("$command0 > /dev/null"); print "System status info is $? \n"; system("$command1 > /dev/null 2> /dev/null"); print "System status info is $? \n"; }
I got 99 problems, but a @%$()_ ain't one.

Replies are listed 'Best First'.
Re: system() status weirdness
by pc88mxer (Vicar) on Mar 28, 2008 at 18:18 UTC
    That's because Net::SSH::Expect is installing the following SIGCHLD handler:
    sub reapChild { do {} while waitpid(-1,WNOHANG) > 0; }
    Doing this will give you your statuses back:
    use Net::SSH::Expect; ... $SIG{CHLD} = 'DEFAULT'; ... system(...);
    I could be wrong, but I doubt the reapChild handler is absolutely necessary, so you're probably safe just use default handler even when you make Net::SSH::Expect calls.
      This works great. Thanks alot ++.

      Bob

      I got 99 problems, but a @%$()_ ain't one.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2022-06-27 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (88 votes). Check out past polls.

    Notices?