时间:2021-05-12 09:10:28 | 栏目:Python代码 | 点击:次
问题
今天用nohup后台执行python程序,并将标准输出和错误输出重定向到一个log文件,但发现log文件隔好久才会更新,很煎熬。。。然而正常屏幕输出时候非常及时。
不确定程序是不是在正常运行。
运行脚本举例:
nohup python test.py > test.log 2>&1 &
原因
python的输出进行了缓冲,导致test.log并不能够马上看到输出。
解决方案
为python添加-u 参数,使得python不启用缓冲。
nohup python -u test.py > test.log 2>&1 &
并实时追踪文件输出到屏幕:
tailf test.log