【发布时间】:2018-09-03 19:07:12
【问题描述】:
当按下 CTLC+C 时,我的脚本通过 except KeyboardInterrupt 退出,如下所示:
....
try:
thread1.start()
thread2.start()
while True:
time.sleep(1)
except (KeyboardInterrupt, SystemExit, Exception):
print("exiting")
# handle proper threads exit
但是,当脚本作为 systemd 运行并且我调用 systemctl stop myscript.service 时,'exiting' 消息不会记录到 syslogger,所以我知道 except 没有被执行。 SystemExit 和 Exception 似乎不会在服务停止时发出。 systemctl 发出了什么,以便我可以在脚本的 try - except 部分中捕获它?
另外,在调用systemctl stop 时我是否需要注意正确的线程退出,或者它会很好地处理我的脚本?
【问题讨论】:
标签: python exception try-catch exit