简体   繁体   English

如何立即在日志文件中获取stdout / stderr?

[英]How to get stdout/stderr in logfiles immediately?

To run some Python program I use the following bash script: 要运行一些Python程序,我使用以下bash脚本:

python3 /MyProg.py 0>/dev/null 1>>output_log_file 2>error_log_file

However I can get information after the prog is terminated only. 但是,仅在编终止后,我才能获取信息。 When program is running there are not any data in these files. 程序运行时,这些文件中没有任何数据。

Can I override this behavior to get log data immediately? 我可以重写此行为以立即获取日志数据吗? My OS is Raspbian. 我的操作系统是Raspbian。

I suspect the program needs to flush stdout and stderr periodically, via 我怀疑程序需要通过以下方式定期刷新stdout和stderr

import sys
sys.stdout.flush()
sys.stderr.flush()

It seems there is because it uses full buffering instead of line buffering. 似乎是因为它使用完全缓冲而不是行缓冲。 Try to use unbuffer before your bash command and you should get the output on screen immediately. 在您的bash命令之前尝试使用unbuffer ,您应该立即在屏幕上显示输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM