Question

I presume closing a terminal window (or a terminal window embedded in an IDE) sends some kind of OS interrupt signal to the process running in the terminal. How can I find out what this signal is? I am looking for a way to capture the interrupt, run some clean up, and then abort. I am using Python and Windows.

Was it helpful?

Solution

You're looking for SIGHUP

SIGHUP The SIGHUP signal is sent to a process when its controlling terminal is closed. It was originally designed to notify the process of a serial line drop (a hangup). In modern systems, this signal usually means that the controlling pseudo or virtual terminal has been closed.[3] Many daemons will reload their configuration files and reopen their logfiles instead of exiting when receiving this signal.[4] nohup is a command to make a command ignore the signal.

OTHER TIPS

Python does not seem to have an exception for this case. The closest would be SystemExit, however that does not actually capture the interrupt you're looking for.

Windows seems to actually send Ctrl+C before killing the process when you close a terminal, however capturing KeyboardInterrupt doesn't seem to work either. At this point you might want to look into the signal module.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top