我有这个过程:
$ nohup bash start.sh
我想用python捕获实时输出: 输出是随机更新的! 示例输出:
some text
#after 2 sec
another test
#after 10 sec
我想捕获此输出并在出现时立即打印!
我尝试过:
import subprocess
import os
for line in os.popen("tail -f /proc/<pid>/fd/1"):
fields = line.split()
output_line = fields[0]
print(output_line)