编辑:
考虑到您没有启动脚本,我想到的一个解决方案是在使用gem时将$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上的EOF,但您可以运行以下内容:
f = IO::popen('cat', 'w')
f.puts('hello')
# signals EOF to "cat"
f.close