Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to quit the Terminal from the Terminal UNIX
One thing I hate is when you type exit in a Terminal window it closes the window but leaves Terminal.app still running. I wrote this simple little script to solve that. Name it quit, chmod 755 it, and stick it somewhere in your path. Now when you type quit, the whole Terminal app will close, not just the current window.
for i in `ps acx | grep "Terminal" |
awk ' { print $1 ; } ' ` ; do kill -9 $i ; 
done
[robg adds: I think an easier method to accomplish the same result would be to just create an alias in your shell startup script: alias quit 'killall Terminal'. I don't think it would take anything else down with it, and no shell scripting required...]
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (5 votes cast)
 
[7,574 views]  

A script to quit the Terminal from the Terminal | 7 comments | Create New Account
Click here to return to the 'A script to quit the Terminal from the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to quit the Terminal from the Terminal
Authored by: JasonPro on May 04, '04 12:41:46PM

Also, using -9 seems a bit excessive. I would think that the normal default term signal would be enough for Terminal to quit. That is, unless you have it configured to warn you about processes still running in the terminal windows, but you can turn that warning off in Terminal's preferences.



[ Reply to This | # ]
A script to quit the Terminal from the Terminal
Authored by: LC on May 04, '04 06:40:11PM

I figured that a -HUP signal will close 'em ... Larry.
(Yeah, tell everyone never to -KILL !)



[ Reply to This | # ]
A script to quit the Terminal from the Terminal
Authored by: pwharff on May 04, '04 12:50:33PM

Typing "exit" or "quit" plus a return is 5 characters to type!

Just use CMD+Q which is 2 characters to type.

Isn't this easier? Am I missing something?



[ Reply to This | # ]
A script to quit the Terminal from the Terminal
Authored by: notmatt on May 04, '04 01:02:57PM

-9 is definitely excessive, as it doesn't allow the program to free resources it's using when it receives the signal.



[ Reply to This | # ]
A script to quit the Terminal from the Terminal
Authored by: mervTormel on May 04, '04 01:12:33PM

command line history may be toasted by quitting sans shell exit.



[ Reply to This | # ]
A script to quit the Terminal from the Terminal
Authored by: jonbauman on May 04, '04 03:28:53PM

Even doing a kill or killall without the -9 is abrupt. It doesn't allow Terminal.app to do it's normal checking of whether there are processes still running that you may care about. Why not tell the terminal to quit a a more appleish way? Use AppleEvents.

alias quit='/usr/bin/osascript -e "tell application \"terminal\" to quit"'
If you want something that you can always run when you're done with a window, which will quit Terminal.app in the event it was the last window, a larger script might be in order. First create this AppleScript:
tell application "Terminal"
	if (count of (every window whose visible is true)) <= 1 then
		quit
	else
		close window 1
	end if
end tell
Then just alias it to whatever command you want and add osascript to the list of ignored processes under the Processes section of the window settings.

---

jon

[ Reply to This | # ]

Just 'trap' it
Authored by: apparissus on May 05, '04 11:42:52PM
If you don't want to remember to type "quit" instead of "exit", and you're using bash, just add the following to your .bashrc or other shell startup script:
trap '/usr/bin/osascript -e "tell application \"terminal\" to quit"' 0
What's it do? When the shell receives signal 0 (zero), that is, told to exit, it will execute this command as the last thing it does. This allows your shell, etc, to exit gracefully, and asking Terminal.app to exit via applescript makes sure it does the same. In other words, type 'exit', and your shell exits, then Terminal quits, all cleanly and the way nature intended.

Note:You'll need to add login, bash, and osascript to the exclude list under "Prompt before closing window" or terminal will whine at you before exiting. Or you could just choose "Never".

Something similar is surely possible with tcsh...but I have no idea how.

[ Reply to This | # ]