![](/img/trans.png)
[英]How to open multiple terminals and execute command in each terminal then save the output from each terminal to a one variable or a file
[英]Save command output to file and see it on the terminal
我有一个缓慢输出列表的命令。 我希望将此列表保存在文件中,并且还希望它在终端上缓慢生成。
python script.py 2>&1 | tee File.txt
这对我不起作用。 保存命令后,在终端上看不到网站列表。
默认情况下,stdout在进入终端时是行缓冲的,但是在重定向时使用更大的缓冲区,因此tee和终端直到稍后才看到输出。
有关使script.py不缓冲输出的方法,请参见此问题的答案禁用输出缓冲
例如,如果script.py是:
#!/usr/bin/python3
import time
for i in range(5):
print('This is line', i, flush=True)
time.sleep(1)
运行./script.py | tee File.txt
./script.py | tee File.txt
将在执行每一行时将每一行打印到终端(相隔一秒)。
如果删除flush=True
则整个输出都将被缓冲,并且在打印所有内容后5秒后脚本完成之前,不会打印任何内容。
2>&1
将stderr重定向到stdout,因此您可能需要对stderr和stdout应用相同的缓冲。
根据Linux文档项目(TLDP) ,
2>&1#将stderr重定向到stdout。 #错误消息被发送到与标准输出相同的位置。
和,
&>文件名#将stdout和stderr都重定向到文件“文件名”。
因此,要将两者通过管道传输到文件,
Command &> | tee File.txt
或者只是stdout
,
Command | tee File.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.