import syssignaltime def now()return time asctime(def onsignal(signumstackframe)python signal handler print('got signal'signum'at'now()most handlers stay in effect if signum =signal sigchldbut sigchld handler is not print('sigchld caught'#signal signal(signal sigchldonsignalsignum int(sys argv[ ]signal signal(signumonsignalwhile truesignal pause(install signal handler sleep waiting for signals to run this scriptsimply put it in the background and send it signals by typing the kill -signal-number process-id shell command linethis is the shell' equivalent of python' os kill function available on unix-like platforms only process ids are listed in the pid column of ps command results here is this script in action catching signal numbers (reserved for general useand (the unavoidable terminate signal)]python signal-demo py [ ]ps - uid pid ppid stime tty : pts/ : pts/ : pts/ time cmd : : python signal-demo py : : ps - : : -bash ]kill - got signal at sun apr : : ]kill - got signal at sun apr : : ]kill - [ ]killed python signal-demo py and in the following the script catches signal which happens to be sigchld on my linux server signal numbers vary from machine to machineso you should normally use their namesnot their numbers sigchld behavior may vary per platform as well on my cygwin installfor examplesignal can have different meaningand signal is sigchld--on cygwinthe script works as shown on linux here for signal but generates an exception if it tries to install on handler for signal (and cygwin doesn' require reaping in any eventsee the signal module' library manual entry for more details]python signal-demo py [ ]ps - uid pid ppid stime tty network scripting time cmd