我正在使用Popen调用一个将其stdout和stderr持续写入日志文件的shell脚本。有没有办法同时连续输出日志文件(到屏幕上),或者让shell脚本同时写入日志文件和标准输出?
我基本上想在Python中做这样的事情:
cat file 2>&1 | tee -a logfile #"cat file" will be replaced with some script
这里再次将stderr/stdout管道连接到tee命令,它会将输出内容同时写入stdout和我的日志文件。
我知道如何在Python中将stdout和stderr写入日志文件。但是我卡在了如何将它们复制回屏幕上:
subprocess.Popen("cat file", shell=True, stdout=logfile, stderr=logfile)
当然,我可以像这样做,但是否有一种方法可以在不使用tee和shell文件描述符重定向的情况下完成此操作?:subprocess.Popen("cat file 2>&1 | tee -a logfile", shell=True)