import signal def handler(*args, **kwargs): raise KeyboardInterrupt signal.signal(signal.SIGTERM, handler)