ruby 超时和系统命令 [英] ruby timeouts and system commands

查看:53
本文介绍了ruby 超时和系统命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ruby​​ 超时,它调用这样的系统 (bash) 命令..

I have a ruby timeout that calls a system (bash) command like this..

Timeout::timeout(10) {
  `my_bash_command -c12 -o text.txt`
}

但我认为即使ruby线程被中断,实际命令仍然在后台运行..正常吗?我怎样才能杀死它?

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?

推荐答案

我认为你必须手动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 超时和系统命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆