I have a ruby timeout that calls a system (bash) command like this..
Timeout::timeout(10) {
`my_bash_command -c12 -o text.txt`
}
but I think that even if the ruby thread is interrupted, the actual command keeps running in the background.. is it normal? How can I kill it?
I think you have to kill
it manually:
require 'timeout'
puts 'starting process'
pid = Process.spawn('sleep 20')
begin
Timeout.timeout(5) do
puts 'waiting for the process to end'
Process.wait(pid)
puts 'process finished in time'
end
rescue Timeout::Error
puts 'process not finished in time, killing it'
Process.kill('TERM', pid)
end
in order to properly stop spawned process tree (not just the parent process) one should consider something like this:
def exec_with_timeout(cmd, timeout)
pid = Process.spawn(cmd, {[:err,:out] => :close, :pgroup => true})
begin
Timeout.timeout(timeout) do
Process.waitpid(pid, 0)
$?.exitstatus == 0
end
rescue Timeout::Error
Process.kill(15, -Process.getpgid(pid))
false
end
end
this also allows you to track process status
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With