1

I have a python script. How can I do something before the process will be terminated? For example I want to print something on display or write to log file. OS: Windows 11 x64 python version: 3.8.10 I tried this one, but it doesn't work:

import signal
import sys
import time


def handle_iterrupt():
    print("Handling interrupt")
    sys.exit(0)


if __name__ == '__main__':
    signal.signal(signal.SIGTERM, handle_iterrupt)
    for x in range(100):
        print(x)
        time.sleep(1)

Update:
If the process is terminated from the task manager, or cmd is closed, or the process is terminated with taskkill /F /PID {PID}, I want to run some function or write something to a log file.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    First of all, `handle_iterrupt` takes two arguments and you have provided 0, so at the very least you need to modify your function definition or it will never work. Second, what are you doing to generate a `SIGTERM` interrupt? Just try `kill task_number`. – Booboo Jan 23 '22 at 13:58
  • I don't think the `signal` stuff works on Windows (i.e. it's for Unix and Unix-like systems only). – martineau Jan 23 '22 at 15:06
  • @martineau If the process is terminated from the task manager, or cmd is closed, or the process is terminated with `taskkill /F /PID {PID}`, I want to run some function or write something to a log file. – Vlad Efanov Jan 23 '22 at 18:21
  • I don't know about those specific scenarios — i.e. which ones cause "normal interpreter termination" — but the [`atexit`](https://docs.python.org/3/library/atexit.html#module-atexit) module might work in at least some of them. Putting almost the whole program in a `main()` function and calling it inside a `try` / `except` might also work in some of the cases. – martineau Jan 23 '22 at 22:05
  • Turns out that ***some*** types of signals do work on the Windows OS — in the last paragraph in the documentation for [`signal.signal()`](https://docs.python.org/3/library/signal.html#signal.signal) it says "On Windows, `signal()` can only be called with `SIGABRT`, `SIGFPE`, `SIGILL`, `SIGINT`, `SIGSEGV`, `SIGTERM`, or `SIGBREAK`." There's a list of what each of those mean [here](https://docs.python.org/3/library/signal.html#module-contents). – martineau Jan 23 '22 at 22:15
  • There's some [more information](https://pymotw.com/3/signal/index.html) about using the `signal` module including examples of using it from the excellent "Python 3 Module of the Week" book and [website](https://pymotw.com/3/) (since you apparently don't even know how to write a *signal handler* function). – martineau Jan 23 '22 at 22:48
  • There some good information on how signals are implemented on Windows in the answers to the question [How to handle a signal.SIGINT on a Windows OS machine?](https://stackoverflow.com/questions/35772001/how-to-handle-a-signal-sigint-on-a-windows-os-machine) – martineau Jan 23 '22 at 23:01

1 Answers1

0

There is no way to run code when a process is terminated by kill -9 or taskkill /F, as the operating system will directly kill the process, leaving no opportunity to handle it.

You can handle other signals that lead to process termination.

On POSIX systems, these include SIGINT, SIGTERM, SIGQUIT, and SIGHUP.

On Windows, the signals are SIGINT, SIGBREAK, CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT.

It's really tricky to handle these signals correctly, especially for cross-platform applications.

To address this, I have created a package called safe-exit.

The documentation can be found here: https://safe-exit.readthedocs.io/en/latest/

John Zhang
  • 73
  • 4