Eventmachine linux系统进程终止

时间:2015-03-19 13:30:53

标签: eventmachine

我使用Eventmachine运行linux / ubuntu系统命令来运行脚本。有时,正在运行的脚本会挂起/冻结。我的问题是,我怎样才能在事件循环中杀死脚本pid。

我试过以下

EM.system(cmd) do |output,status|
  EM.add_timer(5) do
   Process.kill("Term",status.pid)
  end
end

我从status.pid获取的进程ID不是脚本的进程ID。我不确定EM.system返回哪个进程ID。

1 个答案:

答案 0 :(得分:0)

Eventmachine#system方法接受一个调用proc回调。

def terminate_after_timeout
  proc do |process|
    EM.add_timer(60) do
      Process.kill('TERM', process.get_pid)
    end
  end
end

EM.run {
  EM.system(cmd,terminate_after_timeout) do |out,status|
    puts out
  end
end

这是我发现的为数不多的干净策略之一。系统cmd进程将在给定时间后被杀死,在这种情况下为60秒