Skip to content

Instantly share code, notes, and snippets.

@ericdorsey
Created July 6, 2016 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericdorsey/a123b2501708e2044ccead878b557675 to your computer and use it in GitHub Desktop.
Save ericdorsey/a123b2501708e2044ccead878b557675 to your computer and use it in GitHub Desktop.
Python SIGTERM handling
# https://docs.python.org/2/library/signal.html#signal.signal
import signal
import sys
import os
def signal_term_handler(signal, frame):
if signal == 15:
print("got SIGTERM")
sys.exit(0)
else:
print("signal received: {0}").format(signal)
# Exit "other than normal"
sys.exit(1)
# Monitor for SIGTERM signal
signal.signal(signal.SIGTERM, signal_term_handler)
# Generate a SIGTERM
os.kill(os.getpid(), signal.SIGTERM)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment