Sep-27-2022, 07:40 PM
Determine if keyboard interrupt versus SIGINT trapped?
Not sure there is a way to determine this per se? Looked via Google for method, but the best I could find or rather think of was:
Trap SIGINT and KeyboardInterrupt via signal handler, and test if session is interactive, say via sys.__stdin__.isatty(). But this would not be completely accurate, because if SIGINT is sent to python (session, i.e. executing script) from somewhere else, it still might have been an interactive session versus called via cron or as service emulation under systemd. Adding a test to see if systemd active maybe? Such as if psutil.Process(os.getpid()).ppid() == 1? But that is still not 100% bullet proof, right?
Not sure there is a way to determine this per se? Looked via Google for method, but the best I could find or rather think of was:
Trap SIGINT and KeyboardInterrupt via signal handler, and test if session is interactive, say via sys.__stdin__.isatty(). But this would not be completely accurate, because if SIGINT is sent to python (session, i.e. executing script) from somewhere else, it still might have been an interactive session versus called via cron or as service emulation under systemd. Adding a test to see if systemd active maybe? Such as if psutil.Process(os.getpid()).ppid() == 1? But that is still not 100% bullet proof, right?