Exit status from popen3

Hi,

For some reason the code I had posted earlier did not always work
correctly. I’ve now rearrange the code to work properly as far as
running, however, now I can’t seem to capture the exit status.

Help?

def doit(command)
logfile = "test"
threads = []
fh = File.open(logfile,“w”)
Open3.popen3(command) { |inp,out,err|

  threads <<  Thread.new(out) { |out|
     out.each { |line| puts line; fh.print line }
  }
  threads <<  Thread.new(err) { |err|
     err.each { |line| puts line; fh.print line }
  }
  threads.each { |t| t.join }

}
stat = $? >> 8
fh.close
stat
end

···


Apr 7 IBM announces System/360, 1964
Apr 7 Albert Hofmann synthesizes LSD in Switzerland, 1943
Apr 7 Alewives run, Cape Cod
Apr 7* Omer 10th day

I’d like to add that the exit status here is ALWAYS 255, no matter wht
the command is.

db

···

On Tue, Apr 08, 2003 at 01:56:11AM +0900, Daniel Bretoi wrote:

Hi,

For some reason the code I had posted earlier did not always work
correctly. I’ve now rearrange the code to work properly as far as
running, however, now I can’t seem to capture the exit status.

Help?

def doit(command)
logfile = “test”
threads =
fh = File.open(logfile,“w”)
Open3.popen3(command) { |inp,out,err|

  threads <<  Thread.new(out) { |out|
     out.each { |line| puts line; fh.print line }
  }
  threads <<  Thread.new(err) { |err|
     err.each { |line| puts line; fh.print line }
  }
  threads.each { |t| t.join }

}
stat = $? >> 8
fh.close
stat
end


Apr 7 IBM announces System/360, 1964
Apr 7 Albert Hofmann synthesizes LSD in Switzerland, 1943
Apr 7 Alewives run, Cape Cod
Apr 7* Omer 10th day


Apr 7 IBM announces System/360, 1964
Apr 7 Albert Hofmann synthesizes LSD in Switzerland, 1943
Apr 7 Alewives run, Cape Cod
Apr 7* Omer 10th day

Hi,

···

At Tue, 8 Apr 2003 01:56:11 +0900, Daniel Bretoi wrote:

For some reason the code I had posted earlier did not always work
correctly. I’ve now rearrange the code to work properly as far as
running, however, now I can’t seem to capture the exit status.

Since open3 spawns grand child process, you cannot get the exit
status from the command. This is an issue discussed a couple
times, but it’s pending yet.


Nobu Nakada

I did yet another test:

[~]$ ./popen.rb
/etc/passwd
0

[~]$ cat popen.rb
#!/usr/bin/ruby -w

IO.popen(“ls /etc/passwd”) { |out|
out.each { |o| puts o }
}
puts $? >> 8

[~]$ ./popen3.rb
/etc/passwd
255

[~]$ cat popen3.rb
#!/usr/bin/ruby -w

require ‘open3’
Open3.popen3(“ls /etc/passwd”) { |sin,out,err|
out.each { |o| puts o }
}
puts $? >> 8

As you can see, popen3 doesn’t handle the exit status like popen does.
Is this a bug?

db

···

On Tue, Apr 08, 2003 at 02:28:41AM +0900, Daniel Bretoi wrote:

I’d like to add that the exit status here is ALWAYS 255, no matter wht
the command is.

db

On Tue, Apr 08, 2003 at 01:56:11AM +0900, Daniel Bretoi wrote:

Hi,

For some reason the code I had posted earlier did not always work
correctly. I’ve now rearrange the code to work properly as far as
running, however, now I can’t seem to capture the exit status.

Help?

def doit(command)
logfile = “test”
threads =
fh = File.open(logfile,“w”)
Open3.popen3(command) { |inp,out,err|

  threads <<  Thread.new(out) { |out|
     out.each { |line| puts line; fh.print line }
  }
  threads <<  Thread.new(err) { |err|
     err.each { |line| puts line; fh.print line }
  }
  threads.each { |t| t.join }

}
stat = $? >> 8
fh.close
stat
end


Apr 7 IBM announces System/360, 1964
Apr 7 Albert Hofmann synthesizes LSD in Switzerland, 1943
Apr 7 Alewives run, Cape Cod
Apr 7* Omer 10th day


Apr 7 IBM announces System/360, 1964
Apr 7 Albert Hofmann synthesizes LSD in Switzerland, 1943
Apr 7 Alewives run, Cape Cod
Apr 7* Omer 10th day


Apr 7 IBM announces System/360, 1964
Apr 7 Albert Hofmann synthesizes LSD in Switzerland, 1943
Apr 7 Alewives run, Cape Cod
Apr 7* Omer 10th day

As a work around, I end up calling popen3 like this:

Open3.popen3 (“/some/command/to/run ; echo $? 1>&2”) { … }

That ensures the exit code from /some/command/to/run will be the last
line of the standard error stream from Open3.popen3. Then I can just
pull that line off the output to get the return code.

It may not work for all situations, but it works for me.

Jeff.

···

On Monday, April 7, 2003, at 10:53 AM, nobu.nokada@softhome.net wrote:

Hi,

At Tue, 8 Apr 2003 01:56:11 +0900, > Daniel Bretoi wrote:

For some reason the code I had posted earlier did not always work
correctly. I’ve now rearrange the code to work properly as far as
running, however, now I can’t seem to capture the exit status.

Since open3 spawns grand child process, you cannot get the exit
status from the command. This is an issue discussed a couple
times, but it’s pending yet.


Nobu Nakada

Why is it spawning a grandchild process? I got rid of that and it
appears to work just as well. (changing the exit! to exit! $?>>8)

db

···

On Tue, Apr 08, 2003 at 02:53:47AM +0900, nobu.nokada@softhome.net wrote:

Hi,

At Tue, 8 Apr 2003 01:56:11 +0900, > Daniel Bretoi wrote:

For some reason the code I had posted earlier did not always work
correctly. I’ve now rearrange the code to work properly as far as
running, however, now I can’t seem to capture the exit status.

Since open3 spawns grand child process, you cannot get the exit
status from the command. This is an issue discussed a couple
times, but it’s pending yet.


Nobu Nakada


Apr 7 IBM announces System/360, 1964
Apr 7 Albert Hofmann synthesizes LSD in Switzerland, 1943
Apr 7 Alewives run, Cape Cod
Apr 7* Omer 10th day

Hi,

Why is it spawning a grandchild process?

To get rid of leave defunct processes.

I got rid of that and it
appears to work just as well. (changing the exit! to exit! $?>>8)

Possibly, you ran another subprocess before popen3?

$ ruby -I/tmp -ropen3 -e ‘Open3.popen3(“ls”); p $?’
/tmp/open3.rb:35:in popen3': undefined method >>’ for nil (NoMethodError)
from /tmp/open3.rb:17:in fork' from /tmp/open3.rb:36:in popen3’
from -e:1
256

I’m not sure thread/method local variables should be also

process local.

···

At Tue, 8 Apr 2003 03:20:31 +0900, Daniel Bretoi wrote:


Nobu Nakada