Skip to content

Instantly share code, notes, and snippets.

@eshleebien
Created July 9, 2019 07:31
Show Gist options
  • Save eshleebien/98fc2b90ff4bcd7e3e6ae5ce14f55219 to your computer and use it in GitHub Desktop.
Save eshleebien/98fc2b90ff4bcd7e3e6ae5ce14f55219 to your computer and use it in GitHub Desktop.
For my medium article "Containers: Terminating with grace"
# source code
# shamelessly copied from
# https://stackoverflow.com/a/31464349/2591014
import signal
import time
class GracefulKiller:
kill_now = False
signals = {
signal.SIGINT: 'SIGINT',
signal.SIGTERM: 'SIGTERM'
}
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, signum, frame):
print("\nReceived {} signal".format(self.signals[signum]))
print("Cleaning up resources. End of the program")
self.kill_now = True
if __name__ == '__main__':
killer = GracefulKiller()
print("Running ...")
while not killer.kill_now:
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment