From: Dave Thomas Date: 2000-12-19T05:25:21+09:00 Subject: [ruby-talk:7598] Re: timeout question ptkwt@user2.teleport.com (Phil Tomson) writes: > I need to be able to timeout of an operation that has hung (typically, > it's some command sent to the system [ie. system("someprog")] that I need > to timeout of after a few minutes). I found the timeout command in the > book (pg 464) and tried it with a toy program: > > require "timeout" > returnval = timeout(1.5) do > sleep(30) > end > print "I'm awake!\n" > print "return value is: #{returnval}\n" > > It times out fine, but the problem is that the print commands never get > executed either - the timeout seems to kill not only the operation that > was being timed, but the whole script. You'll need to catch TimeoutError require "timeout" begin timeout(2) do puts "starting to sleep" sleep 10 puts "back from sleep" end rescue TimeoutError puts "Didn't wake up soon enough" end => starting to sleep Didn't wake up soon enough > In the case of a system call that is being timed, does the process that > was spawned on the system actually get killed? Not using system(), as the thread is killed only when system returns. I think you'll need to use something lower level, such as fork() and exec(), to give the control you need. Regards Dave