OSError when setting SIGKILL handler in mac os and python

3.5k views Asked by At

Hello I am trying to set up a SIGKILL handling signal with python's signal module, but getting an OSError exception.

Python: 3.7.5

OS: MacOS Mojave

class MyClass:
    def __init__(self, *args, **kwargs):
        signal.signal(signal.SIGKILL, self.gracefull_shutdown)
        signal.signal(signal.SIGTERM, self.gracefull_shutdown)


    def gracefull_shutdown(self, signum, frame):
        # gracefull_shutdown code here

which gives me the following

signal.signal(signal.SIGKILL, self.gracefull_shutdown)
  File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
OSError: [Errno 22] Invalid argument

SIGTERM handling works, (by commenting out the SIGKILL handler). I read that MacOS supports SIGKILL, isn't that right?

1

There are 1 answers

0
HQW.ang On

From the man 7 signal, it says

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

And also please refer to how to handle os.system sigkill signal inside python?