¿linux tee no funciona con python?


Hice un script python que se comunica con un servidor web usando un bucle infinito. Quiero registrar todos los datos de comunicación en un archivo y también monitorearlos desde el terminal al mismo tiempo. así que usé el comando tee como este.

python client.py | tee logfile

Sin embargo, no obtuve nada de la terminal ni del archivo de registro. el script python está funcionando bien. ¿qué está pasando aquí? me estoy perdiendo algo?

Se agradecería algún consejo. Muchas gracias de antemano.

Author: daehee, 2014-02-09

1 answers

De 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.

Así que lo que puedes hacer es:

/usr/bin/python -u client.py >> logfile 2>&1

O usando tee:

python -u client.py | tee logfile
 122
Author: Vor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-09 18:21:24