# Return code of process from IO

**URL:** <https://www.ruby-forum.com/t/return-code-of-process-from-io/55661>\
**Category:** Ruby\
**Created:** [March 22, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/return-code-of-process-from-io/55661 "2006-03-22T21:09:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 22, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/return-code-of-process-from-io/55661/1 "2006-03-22T21:09:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 22, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/return-code-of-process-from-io/55661/2 "2006-03-22T21:36:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 22, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/return-code-of-process-from-io/55661/3 "2006-03-22T22:05:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 22, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/return-code-of-process-from-io/55661/4 "2006-03-22T22:13:55Z")

</div>

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)}”  
}

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 22, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/return-code-of-process-from-io/55661/5 "2006-03-22T22:09:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Farrel\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9d8465/32.png) [@Farrel\_L](https://www.ruby-forum.com/u/Farrel_L)\
**Post date:** [March 22, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/return-code-of-process-from-io/55661/6 "2006-03-22T22:24:05Z")

</div>

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}
