Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

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

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer
    
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.

share|improve this answer
    
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 Sep 23 '10 at 19:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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