假设我有一个从 ruby 脚本启动的进程,如下所示:
pid = spawn('./my_awesome_process')
Process.detach(pid)
这个进程监听 STDIN。 我现在希望读取命令行输入,并像这样将其发送到进程:
puts "Please enter input"
input = gets
然后如何将输入转发到我的流程?
最佳答案
您可以使用 IO.popen
,它将子进程的 stdin
和 stdout
附加到 IO
对象。
IO.popen('rev', 'r+') do |io|
io.puts "I'm looking at the man in the mirror"
io.close_write
puts io.gets
end
rorrim eht ni nam eht ta gnikool m'I
关于ruby - 使用 Ruby 向子进程发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33863943/