gpt4 book ai didi

python - sys.exit(1) 不工作,脚本以代码 0 结尾

转载 作者:行者123 更新时间:2023-12-01 23:54:36 24 4
gpt4 key购买 nike

我有以下代码:

try:
self._collect_persons_status()
except manager.AsteriskManagerError:
# If it is not possible to continue with the collection of initial
# person statuses via Asterisk Manager, end the program with an
# error code.
logger.debug('*** exit with 1!!')
sys.exit(1)

此脚本通过 systemd 处理(作为带有循环的守护进程运行)。

从日志中我可以看到打印了 *** exit with 1!!,但脚本以代码 0 结束,而不是预期的 1:

script-exit-code

我做错了什么?

最佳答案

如果你在脚本顶层有这样的东西,它会阻止 sys.exit() 退出程序:

try:
<invoke script entry point>
except:
<log the error>

这是因为 sys.exit() 是通过引发 SystemExit 异常来实现的。您可以通过将 except: 更改为 except Exception 来修复代码,这不会捕获 SystemExit (以及您可能也不想处理)。

关于python - sys.exit(1) 不工作,脚本以代码 0 结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62991591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com