在后台启动Python进程时,用
nohup python myscript.py > test.log 2>&1 < /dev/null &
Run Code Online (Sandbox Code Playgroud)
问题是stdout缓冲:数据不是实时写入的test.log.以常见的解决办法这个问题是定时冲洗用sys.stdout.flush(),甚至更好,因为提出这个答案,使用python -u:
nohup python -u myscript.py > test.log 2>&1 < /dev/null &
Run Code Online (Sandbox Code Playgroud)
但这还不够.我注意到它在几个小时内工作,然后,它停止工作,即几个小时后test.log不再实时写入,即使我使用过nohup python -u ....
1)这是如何重现问题(我有一个标准的Debian Jessie).启动此文件:
import time
import datetime
while True:
print datetime.datetime.now()
time.sleep(60)
Run Code Online (Sandbox Code Playgroud)
与nohup python -u myscript.py > test.log 2>&1 < /dev/null &.日志文件将在几个小时内更新,然后在3或4小时后更新,不再有任何内容.
2)如何解决这个问题,而不必stdout.flush()在代码中插入每两行(丑陋的解决方案)?