See programcreek
import signal def handler(*xs,**kw): print("handler") signal.signal(signal.SIGHUP,handler)