假设我有类似下面的函数,如何捕获Process.spawn调用的输出?如果进程花费的时间超过指定的超时时间,我也应该能够终止进程.
请注意,该功能还必须是跨平台的(Windows/Linux).
def execute_with_timeout!(command)
begin
pid = Process.spawn(command) # How do I capture output of this process?
status = Timeout::timeout(5) {
Process.wait(pid)
}
rescue Timeout::Error
Process.kill('KILL', pid)
end
end
Run Code Online (Sandbox Code Playgroud)
谢谢.
我想测试一个进程是否正常运行所以我运行:
cmd = "my unix command"
results = `#{cmd}`
Run Code Online (Sandbox Code Playgroud)
如何为命令添加超时,以便如果超过x秒,我可以认为它不起作用?