Da Vinci
Da Vinci

Reputation: 51

do something when terminate a python program

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

Answers (1)

Eric
Eric

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

Related Questions