# Help with timeout and IO.popen (newb)

**URL:** <https://www.ruby-forum.com/t/help-with-timeout-and-io-popen-newb/123227>\
**Category:** Ruby\
**Created:** [December 15, 2007, 3:53am UTC](https://www.ruby-forum.com/t/help-with-timeout-and-io-popen-newb/123227 "2007-12-15T03:53:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [December 15, 2007, 3:53am UTC](https://www.ruby-forum.com/t/help-with-timeout-and-io-popen-newb/123227/1 "2007-12-15T03:53:07Z")

</div>

Hello Folks. I’m running ruby 1.8.6 on Solaris 10 i86pc.

I have a script where I use IO.popen to execute some system  
commands. I want to timeout the execution of the system commands if  
they take too long to execute. So I read about the timeout library  
and have been playing with that. However I think I am missing  
something.

In this example I felt I was setting the timeout to 5 seconds and  
that the script will continue down it’s execution path if the command  
called within IO.popen takes longer than 5 seconds

require ‘timeout’

begin  
timeout(5) {  
IO.popen("/usr/bin/sleep 10") {|f|  
exit\_code = Process.waitpid2(f.pid)[1] \>\> 8  
}  
}  
rescue Timeout::Error  
puts “timed out”  
end

However upon execution the timeout never occurs _and_ the rescue  
clause is executed?

time ./timeouttest1  
timed out

real 0m10.020s  
user 0m0.007s  
sys 0m0.007s

I’ve read the timout documentation and poked through the timeout.rb  
file but I still don’t understand what I’m missing. Any tips are  
appreciated.  
TIA. G.
