我有一个python程序,当shell出现意外时需要清理一些东西,需要捕获什么样的信号?
答案 0 :(得分:2)
使用signal模块为特定信号添加处理程序。例如SIGINT和SIGTERM:
import signal
def handler(signum, frame):
print('Caught signal %d' % signum)
# Now do something (clean-up?) ...
signal.signal(signal.SIGINT, handler)
signal.signal(signal.SIGTERM, handler)
input() # Example to keep the program running