import time import signal def stop( signalNumber, frame ): global keepRunning keepRunning -= 1 print "Ctrl-C pressed; keepRunning is", keepRunning keepRunning = 3 signal.signal( signal.SIGINT, stop ) while keepRunning: print "Executing..." time.sleep( 1 )