Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does bash sometime not flush output of a python program to a file

Tags:

file

bash

flush

I have a crontab job calling a python script and outputting to a file:

python run.py &> current_date.log

now sometimes when I do

tail -f current_date.log

I see the file filling up with the output, but other times the log file exists, but stays empty for a long time. I am sure that the python script is printing stuff right after it starts running, and the log file is created. Any ideas why does it stay empty some of the time?

like image 260
olamundo Avatar asked Oct 16 '09 20:10

olamundo


2 Answers

Python buffers output when it detects that it is not writing to a tty, and so your log file may not receive any output right away. You can configure your script to flush output or you can invoke python with the -u argument to get unbuffered output.

$ python -h

...

-u     : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)
         see man page for details on internal buffering relating to '-u'

...
like image 196
mob Avatar answered Sep 20 '22 18:09

mob


The problem is actually Python (not bash) and is by design. Python buffers output by default. Run python with -u to prevent buffering.

Another suggestion is to create a class (or special function) which calls flush() right after the write to the log file.

like image 42
Steven Avatar answered Sep 17 '22 18:09

Steven