Detecting a hung/stalled child process

J

junk5

Hi all

I need to be able to start an external process (as one might do by
using backquotes, e.g. `ls some-dir`) but then kill that process some
time later if it has not completed. I cannot assume that the program I
call will ever complete (i.e. it potentially has an infinite loop), so
I want to be able to let the program run for 4000 microseconds (or
whatever) and if it hasn't completed, kill it.

I'd be very grateful if anyone could advise me on how to do this. It
would be desirable if the solution was as platform-neutral as possible.

Many thanks in advance.

Chris
 
J

Joel VanderWerf

Hi all

I need to be able to start an external process (as one might do by
using backquotes, e.g. `ls some-dir`) but then kill that process some
time later if it has not completed. I cannot assume that the program I
call will ever complete (i.e. it potentially has an infinite loop), so
I want to be able to let the program run for 4000 microseconds (or
whatever) and if it hasn't completed, kill it.

I'd be very grateful if anyone could advise me on how to do this. It
would be desirable if the solution was as platform-neutral as possible.

Many thanks in advance.

Chris

Someone just put a fork-based library for this on RAA:
http://raa.ruby-lang.org/project/forkedtimeout/
but fork doesn't port well. The following might work, and I'm pretty
sure it works on both windows and linux/unix:

require 'timeout'

Thread.new do
begin
Timeout.timeout(1) do
`sleep 2` # or system "sleep 2"
puts "done"
end
rescue TimeoutError
puts "timed out"
end
end

10.times do
puts "waiting"
sleep 0.5
end

__END__

output:

waiting
waiting
waiting
timed out
waiting
waiting
waiting
waiting
waiting
waiting
waiting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top