Terminal – Closing Unused GNOME Terminals

killterminal

while working I start lots of gnome-terminals and do something with them. dd on first terminal, 12 ssh sessions to a bunch of servers to do something. These sessions are finishing it's job and get disconnected for idle or because the session is closed after the command.

However, I need a script which closes unused gnome-terminal processes. I thought about softly killing all zsh processes without child processes. This will hopefully close the terminal in a clean way.

init─┬─[...]
     ├─gnome-terminal─┬─gnome-pty-helpe
     │                ├─2*[zsh───ssh]
     │                ├─zsh───dd
     │                ├─zsh───pstree
     │                ├─4*[zsh]                 <<<<
     │                └─{gnome-terminal}
     .
     .
     .

I thinking about writing a more or less simple bash script to check the running processes and end them if zsh has no child processes and no activity for 4 hours.

I am unsure

  • If this is a good idea generally.
  • If somebody already published a tool to do this
  • If there is a way to get the child processes of zsh (without greping and awking the pstree output 😉
  • How I get the last activity (thought about a pre and post command in zsh)
  • If there is a better way than kill -HUP <zsh pid> to end the processes

Thanks in advance

Best Answer

According to the zsh manual

TMOUT If this parameter is nonzero, the shell will receive an ALRM signal if a command is not entered within the specified number of seconds after issuing a prompt. If there is a trap on SIGALRM, it will be executed and a new alarm is scheduled using the value of the TMOUT parameter after executing the trap. If no trap is set, and the idle time of the terminal is not less than the value of the TMOUT parameter, zsh terminates. Otherwise a new alarm is scheduled to TMOUT seconds after the last keypress.

I tested

zsh> export TMOUT=10        

and the superior gnome-terminal died when the shell quit. The same mechanism also works within bash. This does address your concerns about nuking busy terminals, as it only starts counting after issuing a shell prompt

I don't think it a good idea generally, as it creates "magical" events which could potentially lose state. I'd recommend getting better with the "close window" and "focus another window (maybe a terminal)" keys of your window manager of choice. But if you do want poofing terminals, this is probably the best way to go.

Related Question