Access STDIN of child process without capturing STDOUT or STDERR

倾然丶 夕夏残阳落幕 提交于 2019-12-04 15:47:53

If you’re on a platform that supports it, you could do this with pipe, fork and exec:

# create a pipe
read_io, write_io = IO.pipe

child = fork do
  # in child

  # close the write end of the pipe
  write_io.close

  # change our stdin to be the read end of the pipe
  STDIN.reopen(read_io)

  # exec the desired command which will keep the stdin just set
  exec 'the_child_process_command'
end

# in parent

# close read end of pipe
read_io.close

# write what we want to the pipe, it will be sent to childs stdin
write_io.write "this will go to child processes stdin"
write_io.close

Process.wait child
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!