【发布时间】:2019-09-07 00:40:40
【问题描述】:
我正在使用 Python 调用带有
的 Shell 脚本def run_command(cmd):
print "Start to run: " + cmd
run = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
line = run.stdout.readline().decode()[:-1]
if line == '' and run.poll() is not None:
break
print line # print the log from shell
recode = run.returncode
if recode != 0:
raise Exception("Error occurs!")
print "End to run: " + cmd
然后我跑
run_command("sh /home/tome/a.sh")
我注意到 a.sh 的控制台输出不是实时的,看起来有一个用于 stdout 的缓冲区,当 stdout 缓冲区已满时,就会打印输出。
我会问如何在我的脚本 a.sh 中禁用 shell 标准输出缓冲区
谢谢!
【问题讨论】: