I use the following setup:
I have an alias that connects using SSH to my server of choice, starting/reconnecting screen as needed. I also remap the Ctrl-A shortcut to Ctrl-Z because I use that one much less often and it's easy to type.
alias myscreen='TERM=xterm ssh myserver -a -x -t /opt/local/bin/screen -xRR -A -e^Zz -U -O'
I can then open a terminal and type myscreen
, and I will connect to myserver
. I can even open multiple terminals, type myscreen
multiple times and get multiple terminals connected to the same screen. I use this often, showing one screen terminal in one window and another in the other.
If you want to reconnect automatically you can do things like
$ while :; do myscreen; done
or even
$ while :; do while ! ping -c1 -t1 myserver >/dev/null; do sleep 10; done; myscreen; echo Connection dropped at $(date); sleep 5; done
Note that all the above is on OS X connecting to Solaris so there may be subtle differences in the syntax of ping and the location of screen.
One more thing: You can customize screen quite a bit, I'm especially fond of these bits of my .screenrc (on the remote side):
# A nice status line at the bottom
hardstatus alwayslastline "%-w%{= BW}%50>%n %t%{-}%+w %<%=|%h"
# special xterm hardstatus: use the window title.
termcapinfo xterm 'hs:ts=\E]2;:fs=\007:ds=\E]2;screen\007'
# Do not use xterm's alternative window buffer, it breaks scrollback
termcapinfo xterm*|xs ti=\E7\E[?47l