Home Page
Posts > Automatic disconnect protection for SSH terminals
Posts > Automatic disconnect protection for SSH terminals
Search:
RABiD BUNNY FEVER
K.T.K

Warning: you do not have javascript enabled. This WILL cause layout glitches.
Automatic disconnect protection for SSH terminals
A simple solution for a simple problem

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:
  1. 0: A new session is created
  2. 1: The session is attached
  3. 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.


Comments
To add comments, please go to the forum page for this post (guest comments are allowed for the Projects, Posts, and Updates Forums).
Comments are owned by the user who posted them. We accept no responsibility for the contents of these comments.

No comments for this Post