Exit status, stderr and stdout

require “open3”
in,out,err = Open3.popen3(“some command”)

Not Windows friendly…

Regards,

Dan

···

-----Original Message-----
From: Daniel Bretoi [mailto:lists@debonair.net]
Hi all,

How would I go about retreiving the following information for
a system-call?

stderr output
stdout output
exit status

IO.popen?
exec?
system?

Though see http://www.ruby-lang.org/raa/list.rhtml?name=win32_popen

martin

···

Berger, Daniel djberge@qwest.com wrote:

require “open3”
in,out,err = Open3.popen3(“some command”)

Not Windows friendly…

Hi Thank you,

How do I get the exit status for this? I’m only getting stdout and
stderr for this.

db

···

On Thu, Jan 23, 2003 at 06:59:56AM +0900, Berger, Daniel wrote:

-----Original Message-----
From: Daniel Bretoi [mailto:lists@debonair.net]
Hi all,

How would I go about retreiving the following information for
a system-call?

stderr output
stdout output
exit status

IO.popen?
exec?
system?

require “open3”
in,out,err = Open3.popen3(“some command”)

Not Windows friendly…

Regards,

Dan


Jan 23 Ernst Abbe born, 1840, formulated diffraction theory
Jan 23 Humphrey Bogart born in New York City, 1899
Jan 23 John Hancock born, 1737
Jan 23 Joseph Hewes born, 1730
Jan 23 Samuel Barber died, 1981
Jan 23 Feast of St. Ildefonsus
Jan 23 National Handwriting Day

Hi,

···

At Fri, 24 Jan 2003 03:09:01 +0900, Daniel Bretoi wrote:

How do I get the exit status for this? I’m only getting stdout and
stderr for this.

$?


Nobu Nakada

This is what I was looking for but I can’t seem to be getting the
appropriate exit value anyway. I tried perl’s $? & 127 which didn’t give
it to me either:

min,out,err = Open3.popen3(“ls /nonexistantdir”);
p $? & 127

=> 0

[~]$ ls /nonexistantdir
ls: /nonexistantdir: No such file or directory
[~]$ echo $?
1

$? on it’s own returns 65280

why the difference?

db

···

On Fri, Jan 24, 2003 at 04:01:11AM +0900, nobu.nokada@softhome.net wrote:

Hi,

At Fri, 24 Jan 2003 03:09:01 +0900, > Daniel Bretoi wrote:

How do I get the exit status for this? I’m only getting stdout and
stderr for this.

$?


Nobu Nakada


Jan 23 Ernst Abbe born, 1840, formulated diffraction theory
Jan 23 Humphrey Bogart born in New York City, 1899
Jan 23 John Hancock born, 1737
Jan 23 Joseph Hewes born, 1730
Jan 23 Samuel Barber died, 1981
Jan 23 Feast of St. Ildefonsus
Jan 23 National Handwriting Day

Hi,

···

At Fri, 24 Jan 2003 04:33:26 +0900, Daniel Bretoi wrote:

This is what I was looking for but I can’t seem to be getting the
appropriate exit value anyway. I tried perl’s $? & 127 which didn’t give
it to me either:

AFAIK, masking $? with 127 is to get the terminating signal.
To get child’s exit status, you should right-shift 8 bit. Or
in 1.7 lator, $? is an instance of Process::Status (unless
nil), and has #exitstatus method.


Nobu Nakada

I had tried rightshifting also, but I get 255 for exit 0 and exit 1.

still confused,

db

···

On Fri, Jan 24, 2003 at 05:02:04AM +0900, nobu.nokada@softhome.net wrote:

Hi,

At Fri, 24 Jan 2003 04:33:26 +0900, > Daniel Bretoi wrote:

This is what I was looking for but I can’t seem to be getting the
appropriate exit value anyway. I tried perl’s $? & 127 which didn’t give
it to me either:

AFAIK, masking $? with 127 is to get the terminating signal.
To get child’s exit status, you should right-shift 8 bit. Or
in 1.7 lator, $? is an instance of Process::Status (unless
nil), and has #exitstatus method.


Nobu Nakada


Jan 23 Ernst Abbe born, 1840, formulated diffraction theory
Jan 23 Humphrey Bogart born in New York City, 1899
Jan 23 John Hancock born, 1737
Jan 23 Joseph Hewes born, 1730
Jan 23 Samuel Barber died, 1981
Jan 23 Feast of St. Ildefonsus
Jan 23 National Handwriting Day

So here’s what’s happening

min,out,err = Open3.popen3(“ls —”);
p $? >> 8

=> 255

system “ls —”
p $? >> 8

=> 1

Why am I not getting the right thing with popen3? I still need a way to
be able to access stderr, stdout and exit status. Can any one offer
alternatives?

db

···

On Fri, Jan 24, 2003 at 05:02:04AM +0900, nobu.nokada@softhome.net wrote:

Hi,

At Fri, 24 Jan 2003 04:33:26 +0900, > Daniel Bretoi wrote:

This is what I was looking for but I can’t seem to be getting the
appropriate exit value anyway. I tried perl’s $? & 127 which didn’t give
it to me either:

AFAIK, masking $? with 127 is to get the terminating signal.
To get child’s exit status, you should right-shift 8 bit. Or
in 1.7 lator, $? is an instance of Process::Status (unless
nil), and has #exitstatus method.


Nobu Nakada


Jan 23 Ernst Abbe born, 1840, formulated diffraction theory
Jan 23 Humphrey Bogart born in New York City, 1899
Jan 23 John Hancock born, 1737
Jan 23 Joseph Hewes born, 1730
Jan 23 Samuel Barber died, 1981
Jan 23 Feast of St. Ildefonsus
Jan 23 National Handwriting Day

Hi,

min,out,err = Open3.popen3(“ls —”);

Did you “Process.wait” here?

p $? >> 8

=> 255

$ ruby -ropen3 -e ‘min,out,err = Open3.popen3(“ls —”);p $?’
nil
[nobu@sharui:~/src/ruby/rough/lib/testunit 07:42:40]
$ ruby -ropen3 -e ‘min,out,err = Open3.popen3(“ls —”);Process.wait;p $?>>8’
1

$? won’t be set until Process.wait return.

···

At Fri, 24 Jan 2003 07:26:34 +0900, Daniel Bretoi wrote:


Nobu Nakada

Hi Nobu,

Thanks for clarifying these things for me.

Is there documentatino for popen3 I can reference?

I;m still not able to obtain the results you are, however:

[~]$ ruby -v
ruby 1.6.8 (2002-12-24) [i386-linux]

[~]$ ruby -ropen3 -e ‘min,out,err = Open3.popen3(“ls
—”);Process.wait;p $?>>8’
-e:1:in `wait’: No child processes (Errno::ECHILD)
from -e:1

and same thing for:

[~]$ ruby1.7 -ropen3 -e ‘min,out,err = Open3.popen3(“ls
—”);Process.wait;p $?>>8’
-e:1:in `wait’: No child processes (Errno::ECHILD)
from -e:1

Which version of ruby are you running? Any idea what might be wrong
here?

Daniel

···

On Fri, Jan 24, 2003 at 07:45:15AM +0900, nobu.nokada@softhome.net wrote:

Hi,

At Fri, 24 Jan 2003 07:26:34 +0900, > Daniel Bretoi wrote:

min,out,err = Open3.popen3(“ls —”);

Did you “Process.wait” here?

p $? >> 8

=> 255

$ ruby -ropen3 -e ‘min,out,err = Open3.popen3(“ls —”);p $?’
nil
[nobu@sharui:~/src/ruby/rough/lib/testunit 07:42:40]
$ ruby -ropen3 -e ‘min,out,err = Open3.popen3(“ls —”);Process.wait;p $?>>8’
1

$? won’t be set until Process.wait return.


Nobu Nakada


Jan 23 Ernst Abbe born, 1840, formulated diffraction theory
Jan 23 Humphrey Bogart born in New York City, 1899
Jan 23 John Hancock born, 1737
Jan 23 Joseph Hewes born, 1730
Jan 23 Samuel Barber died, 1981
Jan 23 Feast of St. Ildefonsus
Jan 23 National Handwriting Day

In perl, I was able to do this,

my $pid = open3($wtr, $rdr, $err, “ls —”);
waitpid($pid,WNOHANG);
warn $? >> 8;

however, looking at the code for open3.rb it doesn’t seem possible to
capture the pid (infact, it seems to be completely discarded).

Does anyone know the reason for this?

db

···

On Fri, Jan 24, 2003 at 07:45:15AM +0900, nobu.nokada@softhome.net wrote:

Hi,

At Fri, 24 Jan 2003 07:26:34 +0900, > Daniel Bretoi wrote:

min,out,err = Open3.popen3(“ls —”);

Did you “Process.wait” here?

p $? >> 8

=> 255

$ ruby -ropen3 -e ‘min,out,err = Open3.popen3(“ls —”);p $?’
nil
[nobu@sharui:~/src/ruby/rough/lib/testunit 07:42:40]
$ ruby -ropen3 -e ‘min,out,err = Open3.popen3(“ls —”);Process.wait;p $?>>8’
1

$? won’t be set until Process.wait return.


Nobu Nakada


Jan 23 Ernst Abbe born, 1840, formulated diffraction theory
Jan 23 Humphrey Bogart born in New York City, 1899
Jan 23 John Hancock born, 1737
Jan 23 Joseph Hewes born, 1730
Jan 23 Samuel Barber died, 1981
Jan 23 Feast of St. Ildefonsus
Jan 23 National Handwriting Day

Hi,

open3.rb (2.09 KB)

···

At Fri, 24 Jan 2003 08:33:11 +0900, Daniel Bretoi wrote:

In perl, I was able to do this,

my $pid = open3($wtr, $rdr, $err, “ls —”);
waitpid($pid,WNOHANG);
warn $? >> 8;

however, looking at the code for open3.rb it doesn’t seem possible to
capture the pid (infact, it seems to be completely discarded).

Sorry, I’ve forgotten that I’m using modified version of
open3.rb, due to get rid of that very problem.

Hm, do you know if anyone intends to fix this or if it has been fixed in
newer versions?

I ended up having to do it in perl because of the lack of ability to do
that.

db

···

On Fri, Jan 24, 2003 at 10:31:23AM +0900, nobu.nokada@softhome.net wrote:

Hi,

At Fri, 24 Jan 2003 08:33:11 +0900, > Daniel Bretoi wrote:

In perl, I was able to do this,

my $pid = open3($wtr, $rdr, $err, “ls —”);
waitpid($pid,WNOHANG);
warn $? >> 8;

however, looking at the code for open3.rb it doesn’t seem possible to
capture the pid (infact, it seems to be completely discarded).

Sorry, I’ve forgotten that I’m using modified version of
open3.rb, due to get rid of that very problem.


Nobu Nakada


Jan 24 John Belushi is born in Chicago, 1949
Jan 25 Robert Burns born, 1759
Jan 25 Virginia Woolf born, 1882
Jan 25 W. Somerset Maugham born, 1874
Jan 24 DG Nova introduced, 1969
Jan 25 First U.S. meeting of ALGOL definition committee, 1958
Jan 26 EDVAC demonstrated, 1952
Jan 24 Eskimo Pie patented by Christian Nelson, 1922
Jan 24 Gold discovered in California at Sutter’s Mill, 1848
Jan 26 Sydney Aust. settled, 1788
Jan 24 Economic Liberation Day in Togo
Jan 26 Republic Day in India
Jan 26 Australia Day in Australia
Jan 24 Warren Zevon is born, 1947
Jan 25 Bob Dylan plays the second “Hurricane” benefit, in the Astrodome, 1978
Jan 26* Parashat Be-Shallah
Jan 26* Shabbat Shirah

Hi,

···

At Sat, 25 Jan 2003 04:09:53 +0900, Daniel Bretoi wrote:

Hm, do you know if anyone intends to fix this or if it has been fixed in
newer versions?

Previously attached one sometimes had left a zombie.

— lib/open3.rb Tue Jan 29 18:17:53 2002
+++ lib/open3.rb Sat Jan 25 17:54:33 2003
@@ -8,6 +8,6 @@

-module Open3

  • class Process < Module
    +module Process
  • class Child < Module
    def initialize(cmd, error = true)
    super(&nil)
    @@ -55,26 +55,33 @@ module Open3
    ensure
    pi.each {|p| p.close unless p.closed?}
  • begin Process.waitpid(pid); rescue Errno::ECHILD; end
    
  • self.class.wait(pid)
    

    end
    end
    ObjectSpace.define_finalizer(self, self.class.finalizer(pid))

  •  pi
    

    end

    def wait

  •  status = Process.waitpid(@pid)
    
  •  status = self.class.wait(@pid)
     ObjectSpace.undefine_finalizer(self)
     status
    
  • end
  • protected
  • def self.wait(pid)
  •  ::Process.waitpid(pid)
    
    rescue Errno::ECHILD
  •  nil
    

    end

    def self.finalizer(pid)

  •  proc {begin Process.waitpid(pid); rescue Errno::ECHILD; end}
    
  •  proc {wait(pid)}
    
    end
    end
    +end

+module Open3
#[stdin, stdout, stderr] = popen3(command);
def popen3(*cmd)

  • p = Process.new(cmd)
  • p = Process::Child.new(cmd)
    [p.writer, p.reader, p.error]
    end


Nobu Nakada