I made a Python script that communicates with a web server using an infinite loop. I want to log every communication data to a file and also monitor them from a terminal at the same time. so, I used a tee command like this.
python client.py | tee logfile
however, I got nothing from terminal or logfile. The Python script is working fine. What is happening here?
From man python
:
-u Force stdin, stdout and stderr to be totally unbuffered. On systems
where it matters, also put stdin, stdout and stderr in binary mode. Note
that there is internal buffering in xreadlines(), readlines() and file-
object iterators ("for line in sys.stdin") which is not influenced by
this option. To work around this, you will want to use "sys.stdin.read‐
line()" inside a "while 1:" loop.
So what you can do is:
/usr/bin/python -u client.py >> logfile 2>&1
Or using tee
:
python -u client.py | tee logfile