In the windows
if you use pywin32, you can execute the event before closing the console, I'm not sure if this will tell you who or what closes it, but maybe this will help you halfway. You can also check: Prevent closing the console application ...
import atexit, os atexit.register(lambda: os.system("pause"))
For those who encounter this and use Linux ...
when the session / SSH window is closed, the SIGHUP signal is called (the signal freezes).
import signal signal.signal( signal.SIGHUP, handler ) def handler(signum, frame):
source share