Repost: Can't sys.exit() from SIGTERM handler?

Jeff Epler jepler at unpythonic.net
Tue Jan 6 02:37:08 CET 2004


If I had to guess, I'd bet you had a blanket exception handler
somewhere.  sys.exit merely raises SystemExit, and if this exception
propogates all the way back up, Python treats it specially by exiting
instead of printing the exception's traceback.

:r term.py
import os, signal, time, sys, traceback

def sigterm(a, b):
	print "sigterm"
	sys.exit()

signal.signal(signal.SIGTERM, sigterm)

os.system("(sleep 1; kill %s) &" % os.getpid())

print "Sleeping (should be killed)"

try:
	time.sleep(2)
except SystemExit:
	traceback.print_exc()
	raise

print "sleep finished (!?)"

:r!python -u term.py

Sleeping (should be killed)
sigterm
Traceback (most recent call last):
  File "term.py", line 14, in ?
    time.sleep(2)
  File "term.py", line 5, in sigterm
    sys.exit()
SystemExit




More information about the Python-list mailing list