Python - Signal Handling

From PeformIQ Upgrade
Jump to navigation Jump to search

Signal Handling in Python

Examples

#!/usr/bin/env python

#=====================================================================

def shutdown():
   sys.exit(99)

#---------------------------------------------------------------------

def sigTerm(signum, frame):
   "SIGTERM handler"

   print "[sig]  Caught SIGTERM!"

   shutdown()

#---------------------------------------------------------------------

def main():
   signal.signal(signal.SIGTERM, sigTerm)

   while 1:
       time.sleep(1)

#---------------------------------------------------------------------

if __name__ == '__main__' or __name__ == sys.argv[0]:
   try:
      sys.exit(main())
   except KeyboardInterrupt, e:
      print "Interrupted!"
      shutdown()

#---------------------------------------------------------------------

"""
Revision History:

     Date     Who   Description
   --------   ---   --------------------------------------------------
   20031016   plh   Initial implementation

Problems to fix:

To Do:

Issues:

"""