def __init__(self):
    signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)

def exit_gracefully(self, signum, frame):
    self.kill_now = True
if __name__ == '__main__':
    killer = GracefulKiller()
while True:
    time.sleep(1)
print('doing something in a loop ...')
if killer.kill_now:
    break
print('End of the program. I was killed gracefully :)')
How to process SIGTERM signal gracefully?