Reputation: 51
I want make a python script do something such as publishing a message before terminating it.
I have tried signal
and it works when I input Ctrl+c
in cmd window.
However, it seems inoperative when I just directly close cmd window or kill the process from Windows task manager. So how can I reach my goal in the condition of above situation?
Thanks for all suggestions!
Upvotes: 5
Views: 465
Reputation: 179
Take a look at the atexit module:
http://docs.python.org/library/atexit.html
Example:
import atexit
def exit_handler():
print('My program just quit')
atexit.register(exit_handler)
Upvotes: 5