1
我有一组需要从Ruby脚本运行的任务,但是一个特定任务总是在退出之前等待STDIN上的EOF。如何在Ruby中打开STDIN过程?
很明显,这会导致脚本在等待子进程结束时挂起。
我有子进程的进程ID,但没有管道或任何类型的句柄。我怎么能打开一个进程的STDIN的句柄来发送EOF?
我有一组需要从Ruby脚本运行的任务,但是一个特定任务总是在退出之前等待STDIN上的EOF。如何在Ruby中打开STDIN过程?
很明显,这会导致脚本在等待子进程结束时挂起。
我有子进程的进程ID,但没有管道或任何类型的句柄。我怎么能打开一个进程的STDIN的句柄来发送EOF?
编辑:鉴于您没有启动脚本,我遇到的解决方案是在您使用您的宝石时将$ stdin置于您的控制之下。我建议是这样的:
old_stdin = $stdin.dup
# note that old_stdin.fileno is non-0.
# create a file handle you can use to signal EOF
new_stdin = File::open('/dev/null', 'r')
# and make $stdin use it, instead.
$stdin.reopen(new_stdin)
new_stdin.close
# note that $stdin.fileno is still 0, though now it's using /dev/null for input.
# replace with the call that runs the external program
system('/bin/cat')
# "cat" will now exit. restore the old state.
$stdin.reopen(old_stdin)
old_stdin.close
如果你的Ruby脚本创建的任务,它可以使用
IO::popen
。例如,
cat
,当带参数运行,将等待EOF标准输入在退出前,但你可以运行以下命令:
f = IO::popen('cat', 'w')
f.puts('hello')
# signals EOF to "cat"
f.close
这不是我的脚本启动的过程中,它实际上是一个宝石。我不想编辑宝石,所以我需要直接发送EOF。 – 2010-11-17 04:48:43
谢谢,这工作! – 2010-11-18 00:52:15