I got tired a long time ago of losing what I was currently working on in SSH sessions when they were lost due to disconnects from network connectivity issues. To combat this I have been using screen when running sessions that I can absolutely not lose, but the problem still persists in other sessions or when I forgot to run it. The easy solution to this was to add screen to one of my bash init scripts (~/.bashrc [or ~/.bash_profile]) as follows:
if [ $TERM == "xterm" ]; then screen -R pts-0.`hostname` && exit; fi
This automatically makes the
screen command run on bash user initialization unless already in
screen (to prevent recursion). From there, 3 things can happen depending on how many detached
screen sessions are currently available:
- 0: A new session is created
- 1: The session is attached
- 2+: The sessions are listed and the user can attach to a specific session by running “screen -r ” followed by its process ID (listed before the “.pts-0”)
Edit on 2010-12-30 @ 3:50am: I changed != "screen" to == "xterm" because otherwise scp and some other non-term programs were failing. You may have to use something other than “xterm” for your systems.
Edit on 2010-1-24 @ 2:00pm: I added the exit; so the terminal automatically exits when the screen session closes.