Skip to content

Instantly share code, notes, and snippets.

@zertrin
Created January 29, 2015 16:20
Show Gist options
  • Save zertrin/62452aefe2fdf78cae26 to your computer and use it in GitHub Desktop.
Save zertrin/62452aefe2fdf78cae26 to your computer and use it in GitHub Desktop.
Cleaning up when killed (Python recipe)
#!/usr/bin/env python
# From http://code.activestate.com/recipes/533117-cleaning-up-when-killed/
from signal import signal, SIGTERM
from sys import exit
import atexit
def cleanup():
print "Cleanup"
if __name__ == "__main__":
from time import sleep
atexit.register(cleanup)
# Normal exit when killed
signal(SIGTERM, lambda signum, stack_frame: exit(1))
sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment