J
Jeremy Hinegardner
require 'timeout'
begin
Timeout.timeout(seconds){ thread.join }
rescue Timeout::Error
thread.kill rescue nil
end
That'll kill the thread, but not the child process that was forked, at least
that's what I remember.
Try this as a general strategy:
parent_t = Thread.new(cmd) do |exec_me|
if cpid = fork then
Thread.current[:cpid] = cpid
cpid, exit_status = Process.waitpid2(cpid)
Thread.current[:exit_status] = exit_status
else
exec exec_me
end
end
unless parent_t.join( seconds ) # seconds contains your timeout value
cpid = parent_t[:cpid]
%w[ TERM KILL ].each do |sig|
Process.kill(sig, cpid)
break if parent_t.join(1)
end
# Do something with Thread.current[:exit_status] to report the child
# process exit status.
enjoy,
-jeremy