Return code of process from IO

F

Farrel Lifson

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
 
F

Farrel Lifson

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

Shea Martin

Farrel said:
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
 
F

Farrel Lifson

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

Shea Martin

Shea said:
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

This should work in a mt env.

IO.popen('dir') { |p|
p.each_line{ |l| puts l }
puts "exitstatus is #{Process.waitpid(p.pid)}"
}
 
S

Shea Martin

Shea said:
This should work in a mt env.

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}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top