When working inside screen over ssh in the Mac OS X Terminal application, it for some reason always hangs after the computer has slept. There’s seems to be no way to resurrect it, other than closing the Terminal and opening a new one, letting the old ssh connection die in loneliness.
However, this problem is avoided by either not using screen (magically, a “normal” ssh connection doesn’t hang the Terminal), or by using autossh to automatically restart a lost connection. I decided to try out the latter option.
I’m daily using my Terminal for both coding and talking to people over IRC. Both of these actions are carried out on a remote server inside a screen session. It’s really annoying when opening my computer after it has slept and I have to start a whole new session starting with opening a new Terminal, ’ssh me@remoteserver.com’, logging in and reattaching the screen. It’s not much, but it’s enough to annoy you if you have to do it tens of times a day.
Autossh came to my rescue. Autossh is a wrapper around the normal ssh, periodically polling the ssh connection to see if it’s lost. The usage is really simple; instead of saying ’ssh me@remoteserver.com’ you simply say ‘autossh -M portnr me@remoteserver.com’. The portnumber is used for polling the underlying ssh connection.
So fine, I solved the main problem, the Terminal hanging. But that wasn’t enough. Writing my username and password all the time was really getting to my nerves, so I solved that by using DSA keys for ssh authentication. But even that was not enough. Writing ‘me@remoteserver.com’ as parameter to autossh was annoying, so I set up a config file for ssh, defining a host ‘remote’ that I configured to use my username and the DSA key that I just created.
Ok, I’m getting there. But still I didn’t like the fact that I had to ‘arrow up’ or write ’screen -dr’ every time autossh reconnected. So I simply added ’screen -dr’ to my .bash_profile on the remote server. This way, when autossh reconnects, my remote bash automatically reattaches the screen session, and all I have to do when awakening my Mac is to wait for autossh to realize that the connection has been lost. Tick tack, and I’m inside my screen session again.





0 Responses to “autossh + screen = fully automatic relogin”
Leave a Reply