编辑:鉴于您没有启动脚本,我遇到的解决方案是在使用您的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在没有参数的情况下运行时,将在stdin退出之前等待EOF,但是您可以运行以下命令:
f = IO::popen('cat', 'w')
f.puts('hello')
# signals EOF to "cat"
f.close