不要等到返回Shell重定向


2

我想将脚本(Python或任何其他类型)的输出重定向到文件。易于使用>或>>。

但是使用这两个输出似乎仅在脚本返回后才写入文件中。从标准输出重定向后,是否可以将输出立即写入文件中?有点像可以访问以读取脚本最后步骤的日志文件。

提前致谢。

Answers:


3

许多程序将缓冲其输出,或者外壳程序将缓冲其输出,因此它不必等到脚本完成后才等待,而是等到缓冲区(通常为4096个字节)已满为止。在脚本中,您可以根据需要手动刷新缓冲区。或者,您可以尝试使用像unbuffer这样的外部软件包。


1
stdbuf在debian coreutils中:stdbuf --output=0 ./script > file
glenn jackman
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.