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.