python - How do I make my script do something when it's killed by the user? -
python - How do I make my script do something when it's killed by the user? -
i want python script when it's killed user. tried using atexit, didn't work. sorry bad english language , thanks.
on posix based systems (os x, linux) can grab user sigint (when press ctrl-c) , sigterm next code:
import signal import sys def signal_term_handler(signal, frame): print 'got sigterm/sigint' sys.exit(0) signal.signal(signal.sigterm, signal_term_handler) signal.signal(signal.sigint, signal_term_handler)
on windows should able do
signal.signal(signal.ctrl_c_event, signal_term_handler) signal.signal(signal.ctrl_break_event, signal_term_handler)
note design there signals cannot grab (sigkill on posix systems) - protect os misbehaving programs.
python python-2.7
Comments
Post a Comment