ruby 超时和系统命令

标签 ruby process timeout terminate

我有一个像这样调用系统(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/

相关文章:

C# 如何通过管理员提升的 cmd 运行进程(带参数)

c++ - 是否可以创建一个包装闭源 C++ 库的 Ruby 扩展?

json - 在 Ruby 中通过路径替换和访问嵌套 hash/json 中的值

ruby-on-rails - Localhost :3000 claims "gem ' pg'"in not installed, Heroku 应用运行正常

ruby - Rspec 的 `should_receive` 是否有侵入性较小的替代方案?

delphi - 像 PsSuspend 一样暂停/恢复进程

python - 为什么我无法从通过 Python 脚本调用的进程中获取输出?

unit-testing - 带超时的单元测试

java - 如何在Tomcat中模拟connectionTimeout?

java - 在 Windows 7 上模拟套接字连接重置/超时