当按下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
时,我是否需要关心正确的线程退出,否则它会很好地处理我的脚本?