import signal def tt(signum, frame): print('You choose to stop me.') exit() signal.signal(signal.SIGINT, tt) signal.signal(signal.SIGTERM, tt) while 1: pass