我正在使用IO.popen启动子进程,但我只得到子进程退出时子进程运行所花费的时间(有时是5分钟或其他)所发生的一切结果.我真正需要的是能够看到的一切子进程写入stderr和stdoutAS-和当它发生.
到目前为止,我找不到任何像这样的东西,但我确信它是可能的.
小智 7
如果你需要实时输出我建议使用stdlib PTY而不是popen
这样的事情:
require 'pty'
cmd = 'echo a; sleep 1; cat /some/file; sleep 1; echo b'
PTY.spawn cmd do |r, w, pid|
begin
r.sync
r.each_line { |l| puts "#{Time.now.strftime('%M:%S')} - #{l.strip}" }
rescue Errno::EIO => e
# simply ignoring this
ensure
::Process.wait pid
end
end
exit "#{cmd} failed" unless $? && $?.exitstatus == 0
> 33:36 - a
> 33:37 - cat: /some/file: No such file or directory
> 33:38 - b
Run Code Online (Sandbox Code Playgroud)
这样你就可以立即获得输出,就像在终端中一样
| 归档时间: |
|
| 查看次数: |
1300 次 |
| 最近记录: |