T
Tim Uckun
I am running a batch process which uses the wkhtmltoimage-i386 binary
to make screenshots of urls. Unfortunately this is in beta and it
frequently hangs up and takes up 100% of one of the CPUs on the
machine.
I have the following code to try and detect the hung process and kill
it but it doesn't always work and I was wondering if anybody has a
better idea of how to do this. When I run it by testing simple
commands like sleep it works perfectly. In production with this binary
it doesn't seem to always work.
def Util.shell_with_timeout(cmd, seconds = 3600)
#the default timeout is an hour. That's probably way too long
Timeout::timeout(seconds) {
@pid, @stdin, @stdout, @stderr = Open4.popen4(cmd)
ignored, @status = Process::waitpid2 @pid
if @status.exitstatus != 0
raise "Exit Status not zero"
end
}
@stdout ? @stdout.read.strip : ''
rescue Timeout::Error
Process.detach @pid
Process.kill 'SIGKILL', @pid
raise "Process Timed out"
rescue => e
msg = @stderr ? @stderr.read.strip : ''
msg += e.to_s
raise "Error during execution of command #{cmd}\n #{msg}"
end
to make screenshots of urls. Unfortunately this is in beta and it
frequently hangs up and takes up 100% of one of the CPUs on the
machine.
I have the following code to try and detect the hung process and kill
it but it doesn't always work and I was wondering if anybody has a
better idea of how to do this. When I run it by testing simple
commands like sleep it works perfectly. In production with this binary
it doesn't seem to always work.
def Util.shell_with_timeout(cmd, seconds = 3600)
#the default timeout is an hour. That's probably way too long
Timeout::timeout(seconds) {
@pid, @stdin, @stdout, @stderr = Open4.popen4(cmd)
ignored, @status = Process::waitpid2 @pid
if @status.exitstatus != 0
raise "Exit Status not zero"
end
}
@stdout ? @stdout.read.strip : ''
rescue Timeout::Error
Process.detach @pid
Process.kill 'SIGKILL', @pid
raise "Process Timed out"
rescue => e
msg = @stderr ? @stderr.read.strip : ''
msg += e.to_s
raise "Error during execution of command #{cmd}\n #{msg}"
end