Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果任何脚本出现错误,如何停止运行多个python脚本_Python_Linux - Fatal编程技术网

如果任何脚本出现错误,如何停止运行多个python脚本

如果任何脚本出现错误,如何停止运行多个python脚本,python,linux,Python,Linux,我有python脚本(installer.py),它运行多个python和shell程序: print "Going to run Script1" os.system("python script1.py") print "Going to run Script2" os.system("python script2.py") 但是我发现,即使script1.py因为错误而没有运行,它也会继续运行script2.py 如何在script1.py本身运行时停止脚本(installer.p

我有python脚本(installer.py),它运行多个python和shell程序:

print "Going to run Script1"

os.system("python script1.py")

print "Going to run Script2"

os.system("python script2.py")
但是我发现,即使script1.py因为错误而没有运行,它也会继续运行script2.py


如何在script1.py本身运行时停止脚本(installer.py)。系统将返回系统调用的退出状态。只需检查您的命令是否正确执行

ret = os.system('python script1.py')
if ret != 0:
    # call failed
    raise Exception("System call failed with error code %d" % ret)


ret = os.system('python script2.py')
if ret != 0:
    # call failed
    raise Exception("System call failed with error code %d" % ret)

os.system
将返回系统调用的退出状态。只需检查您的命令是否正确执行

ret = os.system('python script1.py')
if ret != 0:
    # call failed
    raise Exception("System call failed with error code %d" % ret)


ret = os.system('python script2.py')
if ret != 0:
    # call failed
    raise Exception("System call failed with error code %d" % ret)

为什么您要通过os.system退出,而不是直接导入脚本?您可能需要通过
try
查看错误处理,除了
。为什么您要通过os.system退出,不要直接导入脚本?您可能希望通过
try
查看错误处理,但
除外@suresh请选择一个带勾号的答案,如果它解决了您的原始问题。至于你的后续问题:这听起来更复杂,应该作为一个新的堆栈溢出问题来问(这不仅仅是一个python问题,而是一个系统管理问题)。@suresh如果解决了你原来的问题,请选择一个打勾的答案。至于您的后续问题:这听起来更复杂,应该作为一个新的堆栈溢出问题来问(这不仅仅是一个python问题,而是一个系统管理问题)。