Is it a good idea to put "screen -r" in my .bashrc?

17

9

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?

marcusw

Posted 2010-12-21T19:49:23.780

Reputation: 1 678

Answers

20

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

Gilles 'SO- stop being evil'

Posted 2010-12-21T19:49:23.780

Reputation: 58 319

Isn't it better for ssh to run screen -D -R or even screen -D -RR? – niutech – 2016-01-29T18:47:34.297

Perfect. Should be in the man page. – marcusw – 2010-12-24T02:16:06.317

2

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.

Mikel

Posted 2010-12-21T19:49:23.780

Reputation: 7 890

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 'SO- stop being evil' – 2010-12-21T23:16:21.377

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 – 2010-12-22T00:59:29.853