Return code of process from IO

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

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

···

On 3/22/06, Farrel Lifson <farrel.lifson@gmail.com> wrote:

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

Farrel Lifson 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

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

···

On 3/22/06, Shea Martin <null@void.0> wrote:

Farrel Lifson 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

Shea Martin wrote:

Farrel Lifson 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

This should work in a mt env.

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

Shea Martin wrote:

Shea Martin wrote:

Farrel Lifson 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

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}