【问题标题】:how to stop multiple python script running if any script has error如果任何脚本有错误,如何停止多个 python 脚本运行
【发布时间】:2016-09-14 12:54:40
【问题描述】:

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

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)..

【问题讨论】:

  • 你为什么要通过 os.system 出壳,而不是直接导入脚本?
  • 您可能想通过tryexcept 了解错误处理。

标签: python linux


【解决方案1】:

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)

【讨论】:

  • @suresh 如果它解决了您的原始问题,请选择带有勾号的答案。至于您的后续问题:这听起来涉及更多,应该作为一个新的 Stack Overflow 问题提出(这不仅仅是一个 python 问题,而是误入系统管理的领域)。
猜你喜欢
  • 2013-11-15
  • 2011-01-06
  • 1970-01-01
  • 2019-10-14
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
相关资源
最近更新 更多