I'd like to use screen to keep ssh sessions alive on my server. It would be nice if I could automatically resume any running session for my user when I log in. The straightforward way to do this would be adding "screen -r" to my .bashrc, and this seems to work fine. I'm just wondering if this will break anything under conditions which I haven't tested yet. Anyone with experience here who can tell me whether this is what I should do?
screen -R -d to both automatically attach to an existing session if one exist and create a session otherwise.
Make sure you only do this on interactive shells. Bash sources
~/.bashrc even for non-interactive shells when its parent process is rshd or sshd (this is mentioned in the documentation, but it's easy to miss). You can tell a shell is interactive because
case $- in *i*) screen -Rd;; esac
This won't easily let you run different screen sessions in different terminals.
You'll need to detach from screen, then exit the parent shell to log out. This can be resolved by using
exec screen instead of
If you start a shell other than a login shell, you'll be put into screen, which is not what you'd want most of the time. I would at least restrict this to when you're running directly in an interactive ssh session, with something like the following in your
case "/$(ps -p $PPID -o comm=)" in */sshd) screen -R -d;; esac
Be careful when you do complex things with your
.bashrc as an error that causes the shell to exit would make it difficult to log in.
What I recommend is to not modify your shell initialization files on the server, but instead run screen explicitly from the client, as in
ssh -t host.example.com screen -R -d
(You would probably create a shell alias or desktop environment shortcut on the client.) That way you can easily choose not to run screen, specify an alternate session name and so on.
Your X server startup scripts often start a new shell, e.g. if you're logging in using gdm using the "User Defined Session".
I think anything that doesn't exit might cause your X startup to hang. That would include