当通过 nohup 运行 一个 ruby 脚本时,为什么不写入 nohup.out

When running a ruby script via nohup, why doesn't puts write to nohup.out

我正在尝试 运行 ruby 脚本 nohup:

nohup ruby script.rb &

运行 需要数小时,但通过 puts 记录其进度。

通常,我可以查看 nohup.out 以查看我使用 nohup 运行 的任何最近输出。但是,我的 ruby 脚本在完成或被终止之前似乎不会输出任何内容。

我做错了什么?

我不熟悉通过 nohup 的 运行ning 命令,但是从 "I'm outputting content to a file and it's only being written after the script exits" 类型的问题来看,这些是由缓冲的输出引起的。

所以很可能是 运行 通过 nohup(因此将 puts 输出重定向到 nohup.out)你失去了同步。您可能需要 flush occasionally or enable sync. Since puts 是 "equivalent to $stdout.puts":

$stdout.flush # run this, occasionally
# or just
$stdout.sync = true