LinuxTee不使用python?
我制作了一个python脚本,它使用无限循环与web服务器通信。 我想把每一个通信数据都记录到一个文件中,同时从终端监控它们。所以我像这样使用了tee命令LinuxTee不使用python?,python,linux,tee,Python,Linux,Tee,我制作了一个python脚本,它使用无限循环与web服务器通信。 我想把每一个通信数据都记录到一个文件中,同时从终端监控它们。所以我像这样使用了tee命令 python client.py | tee logfile 然而,我没有从终端或日志文件中得到任何信息。 python脚本运行良好。 这里发生了什么? 我错过什么了吗 如能提供一些建议,将不胜感激。 提前感谢您。来自manpython: -u Force stdin, stdout and stderr to be to
python client.py | tee logfile
然而,我没有从终端或日志文件中得到任何信息。
python脚本运行良好。
这里发生了什么?
我错过什么了吗
如能提供一些建议,将不胜感激。
提前感谢您。来自
manpython
:
-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.
因此,您可以做的是:
/usr/bin/python -u client.py >> logfile 2>&1
或使用T形三通
:
python -u client.py | tee logfile
管道和端子的缓冲行为不同。每当您记录一行时,您可能需要从脚本中执行显式的
sys.stdout.flush()
。对于触发无缓冲输出的其他方法,请参阅另一种方法,即使用script
,这也会禁用缓冲,并另外使控制序列(C-a
、光标键等)工作:。非常好!在我的Raspberry Pi 3上,它也在Python 3中工作,Raspbian Jessie:python3-u client.py | tee logfileA注意:Python和其他各种命令一样,如果stdin和stdout是控制台,将使用行缓冲;如果结果重定向到文件或管道,则使用完全缓冲tee
被视为一个管道(它就是这样),而不是混合体:它向控制台写入数据。注意:这种行为也可以在python程序中控制。另一个注意事项是:python-u client.py | tee>>日志文件
不起作用。>
将引入另一种缓冲写入文件的情况。这就是tee-a
要解决的问题。你也可以把-u放到shebang行中,它就工作了:#/usr/bin/python3-u