Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Scenario

I use terminal to SSH a lot, however would like the Terminal App to quit when the last Tab closes using exit. I already have the terminal window itself set to close on successful exit, however Terminal keeps running. I followed Ricky Campbell's tutorial here which uses an AppleScript to quit terminal. The AppleScript works perfectly when running outside of Terminal, however I somehow can't get the trap command to work that needs to be placed in the terminal.

For reference I will post the relevant text here:

Now we need a way to kickoff the script. This can be done with the bash command “trap”, having it listen for the EXIT signal. Since I want this to happen everytime my user opens a shell, I add it to my .profile file. I do this with nano:

$ nano ~/.profile

In this file add the following somewhere. You may need to edit the path to the AppleScript file you created earlier:

trap '/usr/bin/osascript $HOME/Scripts/QuitTerminal.scpt' EXIT

All this does is listen for the EXIT signal within bash and when it gets it, runs the script we created earlier.

Question

How do I go about troubleshooting this problem. For some reason the trap is not firing or working the way it should, even after numerous reboots. Is there an alternative way to get the Terminal App to close when the last tab exits?

Solution

The links provided by Dori led to the solution. The article suggest that the trap command should be in .profile, when in fact it has to be placed in .bashrc. Moving it to the correct script fixed the problem.

share|improve this question
    
Have you added the trap command to your /Users/YourUsername/.profile ? –  Martín Marconcini Oct 10 '10 at 2:46
    
@Martin Yeah I have. I have confirmed a few time now. –  Diago Oct 10 '10 at 5:47
add comment

4 Answers 4

up vote 4 down vote accepted

The approach given here looks a little simpler—maybe it would work for you?

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.

The full thread can be found here.

share|improve this answer
2  
Thank you. The solution was in there. I had the trap command in .profile and not in .bashrc. Moving it fixed the problem completely and it now works as expected. –  Diago Oct 12 '10 at 7:24
1  
There is also a tab on the Preferences called Shell. If you change the 'When the shell exits:' to 'close if the shell exited cleanly' it will do this without any scripting at all. –  atroon Jan 17 '11 at 17:10
    
@atroon I think that pref applies to closing one Terminal window, not the entire Terminal app. –  Kelvin Apr 28 '11 at 16:07
add comment

You does not need trap any signal.

Simply, put the osascript command into your ~/.bash_logout file (create it, if does not exists)

osascript -e "tell application \"Terminal\" to quit"

The .bash_logout is executed by bash after you logging out. You can enter any shell command into this file, each will executed when you logging out.

For example, you can open a Finder window in the folder where you was last, before exit, with open . or like...

open .
say "Good bye, my master - leaving terminal now, continue with Finder"
osascript -e "tell application \"Terminal\" to quit"

and so on...

share|improve this answer
add comment

I find this url for multi tabed Terminal case. http://cyberdork33.wordpress.com/2010/05/25/make-osx-terminal-quit-when-last-window-closed/
just tested on os x 10.8.2
TL.DR:
for the tab part(exit to close current tab)
Terminal > Preferences > Settings > Shell
When the shell exits: Close if the shell exited cleanly (or Close the window)
Prompt before closing: Never(or Only if there are processes other than the logins shell and: Insert "osascript")

for the last tab part(exit to close this last tab and whole terminal windows and quit Terminal.app):
1) append to ~/.bash_profile:

trap '/usr/bin/osascript $HOME/Scripts/QuitTerminal.scpt' EXIT

2) create Scripts/QuitTerminal.scpt

tell application "Terminal"
    # If there is only one tab remaining, and it contains the word "logout"
    # then this is the final window
    if (count of (tabs of (every window whose visible is true))) = 1 then
        try
            set theContents to words of ((contents of tab 1 of window 1) as Unicode text)
            set exitLastTab to (theContents contains "logout")
        on error
            set exitLastTab to false
        end try

        if exitLastTab is true then
            quit
        end if

    else if (count of (tabs of (every window whose visible is true))) < 1 then
        # If no window remains open, then obviously we can quit the app.
        # This would occur when the final window is closed without 'exit'
        quit
    end if
end tell

Hope it helps.

share|improve this answer
1  
Whilst your answer may theoretically answer the question, it would be preferable to include the essential parts of the article in an answer below, and provide the link for reference –  Pfitz Dec 9 '12 at 8:08
    
@Pfitz agree, and thanks. –  sunqiang Dec 9 '12 at 9:21
add comment

You can also use this convoluted command, which does not trigger a warning about terminating its own process:

osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit

Or, you could define an alias (such as quit or exit) in your profile:

alias quit='osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit'
share|improve this answer
add comment

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.