从长时间运行的进程中捕获并显示stdout文本

时间:2015-07-09 19:59:06

标签: ruby stdout

我正在使用Ruby脚本启动一个长期运行的进程,该进程定期向STDOUT发送信息。例如:

Open3.popen3("tail  -f ./test_file") do |stdin, stdout, stderr|
  # continually read text that the tail command sends to it's STDOUT and
  # send that text to *this* process's STDOUT
  #
  # Tried the following but it doesn't work
  #
  while !stdout.eof?
    puts stdout.read
  end
end

1 个答案:

答案 0 :(得分:0)

#!/usr/bin/env ruby

io_obj = IO.popen('tail -f ./test_file')

while !io_obj.eof?
  # read a line of text at a time
  result = io_obj.readline

  puts result
end