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.
You can suspend and then kill the ssh
process that's hung up. To do this you have to issue the escape sequence, suspend the ssh
process, and then use kill -9
to kill that process.
The default escape key for the ssh that ships with OS X the ~
character. You have to enter it immediately after a new line for ssh to respect it. And then the key sequence Control-z is used to suspend and background a task in bash.
So try this key sequence:
Return
~
Control-z
If it works you'll see something like:
myhost.local:~ |ruby-2.2.0|
> ssh someremotehost
Last login: Fri Mar 6 14:15:28 2015 from myhost
someremotehost:~ |ruby-2.2.0|
> ~^Z [suspend ssh]
[1] + 48895 suspended ssh myremotehost
This line of output:
[1] + 48895 suspended ssh myremotehost
tell you the process ID of the ssh
process on your machine. It's 48895
in this example. That process is still running, it's just been suspended and backgrounded. You need to kill it.
You can do that with the kill
command. You want to kill it with prejudice so use the -9
option when you call kill
like so:
myhost.local:~ |ruby-2.2.0|
> kill -9 48895
[1] + 48895 killed ssh someremotehost
Just use the PID of your ssh
process when you call that command in place of the 48895
PID I used above.
And you'll have your prompt back.
Alternatively, you can open a second Terminal window and use ps
to find the ssh
process in the process list and issue the kill -9
call against the PID. Though, that kind of defeats the process of getting your prompt back in the original Terminal window, doesn't it?
Best Answer
Open Terminal.
Go to Terminal → Preferences….
Select the Settings tab, then your profile, and choose the Shell tab.
Set When the shell exits to Close if the shell exited cleanly.