Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: can we detect the closing of a terminal?



Kurtis, ZyX:


On 09/04/2015 02:37 PM, Kurtis Rader wrote:

I'm guessing that by "open a terminal" you mean start an interactive shell.
When I fire up an xterm it comes with a zsh shell pre loaded. That's all I know. In each one of them you can "echo $TTY" and it will report "/dev/pts/2" or some other number than '2'. I can have a dozen of them open, and when I close one I'd like some way of letting the other terminals know it. Reason being that each terminal has an associated variable 't[number of the terminal]' set to its $PWD and I do stuff like: 'cp * $t10' where '$t10' is the current directory on terminal '/dev/pts/10' and which does horrible things is terminal #10 has been closed so I want some way of killing '$t10' when that happens.

The distinction is important when you consider cases such as using ssh to
log into another system or starting an xterm that simply displays the
output of a command (possibly a zsh script). Or consider the case where
you're using a program such as "screen" or "tmux" to multiplex access to a
shell. In that case it is normal to create a new "terminal" and connect to
an existing tmux session. Then close that "terminal" while leaving the tmux
session, and thus the interactive shell running within it, still running.
Yikes, there's more things you mention there than I even know exist ;-)

The simplest solution is to create a $HOME/.zlogout script (see "man zsh").
Other solutions such as a TRAPEXIT function or using the trap command to
handle SIGHUP and SIGTERM are also possible. The trap command solution is
the most portable.

Between what you say and ZyX's comments, there's a few things to chew on, tho it's mostly way over my head still but I'll give it a dangle and thrash.



Messages sorted by: Reverse Date, Date, Thread, Author