When launching a Python process, in background, with
nohup python myscript.py > test.log 2>&1 < /dev/null &
the problem is that stdout is buffered: the data is not written in realtime to test.log. The common solution to this problem is to flush periodically with sys.stdout.flush(), or even better, as suggested by this answer, to use python -u :
nohup python -u myscript.py > test.log 2>&1 < /dev/null &
But this is not enough. I noticed that it worked during a few hours, and then, it stopped working, i.e. after a few hours test.log is not written in realtime anymore, even if I used nohup python -u ....
1) This is how to reproduce the problem (I have a standard Debian Jessie). Start this file:
import time
import datetime
while True:
print datetime.datetime.now()
time.sleep(60)
with nohup python -u myscript.py > test.log 2>&1 < /dev/null &.
The log file will be updated during a few hours, and then after 3 or 4 hours, nothing anymore.
2) How to solve this problem, without having to insert stdout.flush() every 2 lines in the code (ugly solution) ?