3

I would like to register a custom signal handler that does some things and then continues with the normal signal handling, specifically for the SIGTERM signal.

Python does not have a SIGTERM handler registered (Python: What is the default handling of SIGTERM?), so how can I continue with the OS's normal SIGTERM processing after I do my own custom handling?

def my_handler(self, signum, frame):
    do_something()
    # What do I do here to raise to the OS?

signal.signal(signal.SIGTERM, my_handler)
C_Z_
  • 7,427
  • 5
  • 44
  • 81

2 Answers2

3

If the fatal signal's previous disposition was the default, the best you can do is restore that disposition and re-raise the signal.

This will at least cause your process to terminate abnormally, such that its parent can tell which signal killed it (os.WIFSIGNALED). If the fatal signal has an "additional action" like a core file, this mimicry will not be perfect.

orig_handler = signal.signal(signal.SIGTERM, my_handler)

def my_handler(signum, frame):
  do_something()

  # Now do whatever we'd have done if
  # my_handler had not been installed:

  if callable(orig_handler):              # Call previous handler
    orig_handler(signum, frame)
  elif orig_handler == signal.SIG_DFL:    # Default disposition
    signal.signal(signum, signal.SIG_DFL)
    os.kill(os.getpid(), signum)
                                          # else SIG_IGN - do nothing

Re-raising the signal as above isn't nuanced enough for signals that stop the process (e.g., SIGTSTP) or are ignored by default (SIGCHLD, SIGURG).

pilcrow
  • 56,591
  • 13
  • 94
  • 135
  • The problem is, python does not have a signal handler for SIGTERM – C_Z_ Mar 20 '21 at 17:26
  • @C_Z_ then the signal disposition will be the default, which is SIG_DFL, which you can handle as described above. (The above also handles a program which installs its own handler before your signal handling is set up.) – pilcrow Mar 20 '21 at 19:06
-1

You want to save the default one before, as this:

def my_handler(self, signum, frame):
    global original_sig_handler # Doesn't really matter if you do it as global or something else, you just need  the function to get here
    do_something()
    original_sig_handler()

original_sig_handler = signal.getsignal(signal.SIGINT) # this return the function that called by handling the signal
signal.signal(signal.SIGINT, my_handler)

EDIT:

this will not work with sigterm but will work for few other signals.

probably for sigterm python do something as sys.exit or ect.

Reznik
  • 2,663
  • 1
  • 11
  • 31