我有一个像这样调用系统(bash)命令的 ruby 超时..
Timeout::timeout(10) {
`my_bash_command -c12 -o text.txt`
}
但我认为即使 ruby 线程被中断,实际命令仍在后台运行..这正常吗?我怎样才能杀死它?
最佳答案
我认为你必须手动kill
它:
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
关于ruby 超时和系统命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8292031/