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.

     │                ├─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

  • IIRC that was bug in gnome-terminal - what version are you using? – Maciej Piechotka Sep 23 '10 at 20:50

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.

  • nice, this is much simpler than I thought ;) I think I only set this on my rdesktop run-and-forget shells as with an alias for the command ;) – krissi Sep 23 '10 at 16:10

If you use exec to start your command (e.g. exec ssh *hostname*), the gnome-terminal it's running on should close when the process exits (assuming you have it set to exit the terminal when the command exits, which is the default).

You would still need to use the shell's timeout capability or other smarts for hung processes, but that would take care of normal exits or disconnected remotes if you're just trying to close the terminal sessions.

  • A downside of using exec in this way is that if the command doesn't start for any reason or terminates abruptly, you won't be able to read the error message. – Gilles 'SO- stop being evil' Sep 23 '10 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.