In a terminal I start a background process A, which in turn starts a process B. Process B writes to the terminal (process A has passed B the correct TTY file descriptor).
What I am afraid of, is if the user (in some cases, me) closes the terminal window without sending process A or B a SIGINT. What could then happen is that process B will still attempt to write to the terminal even though it's been closed by the user. Worse, the user could open a new terminal window and it might assume the same identity / file descriptor that the other terminal had and then subsequently get written to by process B.
Basically, I am looking for a way to "listen" for terminal session events like terminal sessions being closed.
Is it possible to listen for such events inside a Node.js process? Perhaps there is a corresponding handler similar to process.on('SIGINT')
?
I was guessing maybe the SIGTERM
event was the event to listen to, but now after experimenting with the code, don't think that is it.