Possible I/O sync issue with Ruby script under noh

2019-05-28 19:45发布

Had an odd issue with the output of a Ruby script running under nohup. Basically, there's a severe output delay when writing to a logfile or with stdout to nohup.out. You can see the original post here, which was answered for that use case:

StackOverflow - Question 19455667

It was suggested that I use something like file_object.sync = true Which works, however I'm interested in the specifics of why this is occurring only when the script is under nohup. I'm guessing there's a deeper complexity of how nohup is buffering it's own shell output. That way I can implement a more elegant solution if I have more verbose output.

Any additional resources appreciated.

Specs: Ruby 1.8.7 Linux: Seen on CentOS 5.4, 5.6, and the Amazon Linux AMI release 2012.03 (Somewhat akin to CentOS 6.2) Nohup: 8.4

标签: linux ruby nohup
1条回答
仙女界的扛把子
2楼-- · 2019-05-28 20:04

This due to the fact when your STDOUT is not connected to tty, but to a file, block-sized buffering is used, if it is connected to tty, line-based buffering is used.

If you ran your command without NOHUP, the output (both stdout and stderr) would still be connected to the same tty, and it would be line-buffered. The default behavior of NOHUP is to write to nohup.out file. Since buffers are generally a lot bigger than lines, it will take much longer to "display" the results.

查看更多
登录 后发表回答