简体   繁体   English

将过程输出实时记录到文件中

[英]Logging process output to file in real time

I have a python script running on startup in the background. 我在后台启动时运行了python脚本。 Its print commands do not come through to the bash console as they do when calling the script manually with eg python script.py . 它的打印命令不会像使用python script.py手动调用脚本时那样传递到bash控制台。 Output and errors can be redirected to file by calling python script.py > log.txt 2>&1 . 可以通过调用python script.py > log.txt 2>&1将输出和错误重定向到文件。 But after startup, as the script runs, I can see the log file but it has zero size, so is presumably waiting for the script to end. 但是启动后,随着脚本的运行,我可以看到日志文件,但是它的大小为零,因此大概是在等待脚本结束。 Calling it with cat returns nothing. cat调用它不会返回任何内容。 I've tried using append >> instead with no luck. 我试过使用append >>而不是碰运气。

Is there any way to make the output happen in real time, so it can be viewed? 有什么方法可以使输出实时发生,以便可以查看?

您的输出正在缓冲中,因此请在Python脚本中刷新它,以确保显示结果:

sys.stdout.flush()

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

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