bash Tee 不显示输出或写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27534609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tee does not show output or write to file
提问by Nate Mara
I wrote a python script to monitor the statuses of some network resources, an infinite pinger if you will. It pings the same 3 nodes forever until it receives a keyboard interrupt. I tried using tee to redirect the output of the program to a file, but it does not work:
我写了一个 python 脚本来监控一些网络资源的状态,如果你愿意的话,一个无限的 pinger。它永远 ping 相同的 3 个节点,直到它收到键盘中断。我尝试使用 tee 将程序的输出重定向到文件,但它不起作用:
λ sudo ./pingster.py
15:43:33 node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:35 node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:36 node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:37 node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:38 node1 SUCESS | node2 SUCESS | node3 SUCESS
^CTraceback (most recent call last):
File "./pingster.py", line 42, in <module>
main()
File "./pingster.py", line 39, in main
sleep(1)
KeyboardInterrupt
λ sudo ./pingster.py | tee ping.log
# wait a few seconds
^CTraceback (most recent call last):
File "./pingster.py", line 42, in <module>
main()
File "./pingster.py", line 39, in main
sleep(1)
KeyboardInterrupt
λ file ping.log
ping.log: empty
I am using coloramafor my output, I thought that perhaps could be causing the issue, but I tried printing something before I even imported colorama, and the file is still empty. What am I doing wrong here?
我正在为我的输出使用colorama,我认为这可能是导致问题的原因,但我什至在导入 colorama 之前尝试打印一些东西,但文件仍然是空的。我在这里做错了什么?
Edit: Here is the python file I'm using
编辑:这是我正在使用的 python 文件
#!/home/nate/py-env/ping/bin/python
from __future__ import print_function
from datetime import datetime
from collections import OrderedDict
from time import sleep
import ping
import colorama
def main():
d = {
'node1': '10.0.0.51',
'node2': '10.0.0.50',
'node3': '10.0.0.52',
}
addresses = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
colorama.init()
while True:
status = []
time = datetime.now().time().strftime('%H:%M:%S')
print(time, end='\t')
for location, ip_address in addresses.items():
loss, max_time, avg_time = ping.quiet_ping(ip_address, timeout=0.5)
if loss < 50:
status.append('{0} SUCESS'.format(location))
else:
status.append(
'{}{} FAIL{}'.format(
colorama.Fore.RED,
location,
colorama.Fore.RESET,
)
)
print(' | '.join(status))
sleep(1)
if __name__ == '__main__':
main()
回答by that other guy
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
当标准输出不是终端时缓冲标准输出。解除缓冲的最简单方法是使用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
).
您还可以将 shebang 设置为#!/usr/bin/python -u
(这不适用于env
)。