Tee does not show output or write to file

Here’s a simpler way of reproducing your issue:

$ cat foo.py
from time import sleep
while True: 
  sleep(2)
  print "hello"

$ python foo.py
hello
hello    
(...)

$ python foo.py | tee log
(no output)

This happens because python buffers stdout when it’s not a terminal. The easiest way to unbuffer it is to use python -u:

$ python -u foo.py | tee log
hello
hello
(...)

You can also set the shebang to #!/usr/bin/python -u (this does not work with env).

Leave a Comment

tech