简体   繁体   中英

Default signal handling in Python

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)

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 (eg, SIGTSTP) or are ignored by default (SIGCHLD, SIGURG).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM