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!
exit() method is used to terminate the process with the specified status. We can use this method without flushing buffers or calling any cleanup handlers. After writing the above code (python os. exit() function), the output will appear as a “ 0 1 2 “.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With