告诉nohup实时写入输出 [英] Telling nohup to write output in real-time
问题描述
使用nohup
时,脚本的输出将被缓冲,并且仅在脚本执行完毕后才转储到日志文件(nohup.out).以接近实时的方式查看脚本输出,以了解脚本的进展情况,这将非常有用.
有没有办法让nohup
在脚本产生输出时写入输出?或者,由于这种频繁的文件访问操作很慢,因此要在执行期间定期转储输出?
When using nohup
the output of a script is buffered and only gets dumped to the log file (nohup.out) after the script has finished executing. It would be really useful to see the script output in something close to real-time, to know how it is progressing.
Is there a way to make nohup
write the output whenever it is produced by the script? Or, since such frequent file access operations are slow, to dump the output periodically during execution?
推荐答案
为此有一个特殊的程序:unbuffer!参见 http://linux.die.net/man/1/unbuffer
There's a special program for this: unbuffer! See http://linux.die.net/man/1/unbuffer
这个想法是,程序的输出例程会认识到stdout不是终端(isatty(stdout) == false
),因此它们会将输出缓冲到最大大小.将unbuffer
程序用作程序的包装程序,将欺骗"它一次写入一行输出,就像直接在交互式终端中运行该程序一样.
The idea is that your program's output routines recognize that stdout is not a terminal (isatty(stdout) == false
), so they buffer output up to some maximum size. Using the unbuffer
program as a wrapper for your program will "trick" it into writing the output one line at a time, as it would do if you ran the program in an interactive terminal directly.
这篇关于告诉nohup实时写入输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!