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?


You need 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 $- contains i.

case $- in
  *i*) screen -Rd;;

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 screen.

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 ~/.bash_profile:

case "/$(ps -p $PPID -o comm=)" in
  */sshd) screen -R -d;;

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.

  • Perfect. Should be in the man page. – marcusw Dec 24 '10 at 2:16
  • Isn't it better for ssh to run screen -D -R or even screen -D -RR? – niutech Jan 29 '16 at 18:47

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 screen -r.

  • X server startup scripts won't run .bashrc. But there are other circumstances where this would be a problem, for example when running ssh host.example.com somecommand (weirdly, bash does run .bashrc then). – Gilles Dec 21 '10 at 23:16
  • It is on some distros if you choose "User Defined Session". On Ubuntu, it sources .profile explicitly, others call .xsession from inside a login shell, which would typically mean .bash_profile, and most users source .bashrc from inside their .bash_profile. – Mikel Dec 22 '10 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.