In python 2.6 under Linux, I can use the following to handle a TERM signal:
import signal
def handleSigTERM():
shutdown()
signal.signal(signal.SIGTERM, handleSigTERM)
Is there any way to setup a handler for all signals received by the process, other than just setting them up one-at-a-time?
If you want to get rid of the try, just ignore signals that cannot be caught.
As of Python 3.5, the signal constants are defined as an enum, enabling a nicer approach:
Here's a 2/3 compatible way which doesn't have as many pitfalls as the others:
Since
signalnum
is just a number, iterate over 1 to out of range setting the signal to a particular handle.You could just loop through the signals in the signal module and set them up.
That code won't work in the current version of python. There are many variables starting with SIG with the same value. For instance, SIGHUP and SIG_UNBLOCK are both 1. The only way I could think of to get a list of actual signals was to just make it myself.
Works on Windows 10 and Python 3.7:
Results: