I have a python process which runs in background, and I would like it to generate some output only when the script is terminated.
def handle_exit():
print(\'
Try signal.signal. It allows to catch any system signal:
import signal
def handle_exit():
print('\nAll files saved in ' + directory)
generate_output()
atexit.register(handle_exit)
signal.signal(signal.SIGTERM, handle_exit)
signal.signal(signal.SIGINT, handle_exit)
Now you can kill {pid}
and handle_exit
will be executed.