0

If a process is running and for example the user accidentally terminates via the Task Manager or the machine reboots (thus forcefully terminating processes), how can I register such an event that the process will execute some task before completely terminating?

What I've tried unsuccessfully is:

from signal import signal
from signal import SIGTERM

def foo():
    print('hello world')

if __name__ == '__main__':
    signal(SIGTERM, foo)
    while True:
        pass

I'll run this from the command line, then navigate to task manager and end the task but foo is never called.

1 Answers1

0

Based on the answer to Can I handle the killing of my windows process through the Task Manager? - it seems like the task manager kills processes using an unmaskable signal (equivalent to Linux's SIGKILL). This means that you cannot catch it.

There are other signals you can catch in Windows, like SIGBREAK, SIGCHLD, CTRL_C_EVENT and CTRL_BREAK_EVENT, but I guess the task manager does not use any of those for terminating a process.

Amitai Irron
  • 1,973
  • 1
  • 12
  • 14