Return code of process from IO


#1

Hi folks,

I’m using IO.popen to communicate with an external process. At some
point the external process will exit either with a true or false
condition. It uses shell return codes (0 for true, 1 for false) to
indicate that this is happened. Is there a way to get the vale of this
return code from an IO object? Or will it be better to use Kernel.` or
suchlike. The $? system var doesn’t seem to be updated using IO.

Farrel


#2

Just solved it. Just run Process.wait after the call to the IO.popen
and $? gets updated.


#3

Farrel L. wrote:

Just solved it. Just run Process.wait after the call to the IO.popen
and $? gets updated.

This doesn’t work in this case:

#!/usr/bin/ruby

Thread.new{
IO.popen(‘ls non_existant_dir’).each_line{|l| puts l}
sleep 10
Process.wait
puts “exit status is #{$?}, should be non-zero”
}

sleep 5

Thread.new{
IO.popen(‘dir’).each_line{|l| puts l}
Process.wait
puts “exit status is #{$?}, should be 0”
}

sleep 15

exit 0

~S


#4

Shea M. wrote:

sleep 10
}

sleep 15

exit 0

~S

This should work in a mt env.

IO.popen(‘dir’) { |p|
p.each_line{ |l| puts l }
puts “exitstatus is #{Process.waitpid(p.pid)}”
}


#5

Yeah that can be a problem, but luckily I’m not using any threads so
it seems to be working…


#6

Shea M. wrote:

IO.popen(‘ls non_existant_dir’).each_line{|l| puts l}
puts “exit status is #{$?}, should be 0”

IO.popen(‘dir’) { |p|
p.each_line{ |l| puts l }
puts “exitstatus is #{Process.waitpid(p.pid)}”
}
that should be #{Process.waitpid2(p.pid)[1].exitstatus}