我编写了python脚本来运行bash脚本,并在此行中运行它:
result = subprocess.Popen(['./test.sh %s %s %s' %(input_file, output_file, master_name)], shell = True)
if result != 0:
print("Sh*t hits the fan at some point")
return
else:
print("Moving further")
现在,当bash脚本失败时,我会遇到麻烦,python不会继续做它正在做的事情,只是结束了。如何使bash失败后继续运行python脚本?
答案 0 :(得分:1)
您忘记了communicate
。除了您return
之外,当bash脚本失败时,难怪python会“停止”。
from subprocess import Popen, PIPE
p = Popen(..., stdout=PIPE, stderr=PIPE)
output, error = p.communicate()
if p.returncode != 0:
print("Sh*t hits the fan at some point %d %s %s" % (p.returncode, output, error))
print("Movign further")