4

I want to start gnu screen automatically when I login to my remote machine via ssh, so I add

exec screen

to the end of the .bash_profile file, then I find out that when I terminate the screening, my connection to the ssh host is also closed immediately. How can I avoid this?

In case that I want to resume a screen (e.g. named 'old-screen'), because I start screening every time when I login, I face a situation that I am attached to 'new-screen' and I want to reattach to the 'old-screen'. If I just

screen -r old-screen

I find myself in a recursive screen, and I can not navigate within 'old-screen' because all the shortcut key are received by 'new-screen'. If I try to quit the current screen, my connect to the remote machine will also be lost immediately.

Any solution to it?

4
  • If you swap -r with -R it'll create a new screen session for you if there is none to resume.
    – Svish
    Mar 9, 2011 at 13:34
  • put 'exec screen -R' in .bash_profile solves my problem. thx
    – zhanwu
    Mar 9, 2011 at 14:01
  • @Svish: Please post your suggestion as an answer so zhanwu can accept it. Mar 9, 2011 at 14:31
  • Added it as an answer :)
    – Svish
    Mar 10, 2011 at 8:36

5 Answers 5

8

Instead of using screen -r which tries to resume a screen session, you can use screen -R which tries to resume a screen session and creates a new one if one doesn't exist.

   -r [pid.tty.host]
   -r sessionowner/[pid.tty.host]
        resumes  a detached screen session.  No other options (except com-
        binations with -d/-D) may be specified, though an optional  prefix
        of  [pid.]tty.host  may  be needed to distinguish between multiple
        detached screen sessions.  The second form is used to  connect  to
        another  user's  screen session which runs in multiuser mode. This
        indicates that screen should look for sessions in  another  user's
        directory. This requires setuid-root.

   -R   attempts to resume the first detached screen session it finds.  If
        successful, all other command-line options  are  ignored.   If  no
        detached  session exists, starts a new session using the specified
        options, just as if -R had not been specified. The option  is  set
        by default if screen is run as a login-shell (actually screen uses
        "-xRR" in that case).  For combinations with the -d/-D option  see
        there.

Personally I tend to use screen -DRA.

   -D -R   Attach here and now. In detail this means: If a session is run-
           ning, then reattach. If necessary detach  and  logout  remotely
           first.   If  it  was not running create it and notify the user.
           This is the author's favorite.
   -A      Adapt  the  sizes of all windows to the size of the current termi-
           nal.  By default, screen tries to restore  its  old  window  sizes
           when  attaching  to  resizable  terminals  (those with "WS" in its
           description, e.g. suncmd or some xterm).
4

The following seems to work for me in my: ~/.bash_profile

## if $STY is not set...
if [ -z "$STY" ]; then
    screen  -d -RR
fi

I took the answer from here: https://superuser.com/a/52329/76204

but instead of creating a new screen each time it reaattaches a session or creates one if necessary. Also it appears that neither ssh connection exits when the screen is reattached or closed.

4
  • What if you break your .screenrc, or /etc/screenrc? At least if you uninstall screen, then the exec will fail and your shell won't exit, but it will if screen runs but just exits with an error. Feb 6, 2015 at 13:54
  • I have not encountered that in 5+ years on 50 or so virtual + real machines. In that case I guess I would have to boot of a live iso and comment out the offending lines in the ~/.bash_profile file.
    – drescherjm
    Feb 6, 2015 at 13:58
  • Yeah, it seems unlikely. If another user on the machine had a non-broken account, or if there was a root password (instead of sudo only) you could fix it that way. Feb 6, 2015 at 14:25
  • 2
    If you break your .screenrc, just ssh ip "bash --noprofile"
    – airstrike
    Mar 3, 2015 at 4:50
1

I have often pondered the same thing myself.

The closest I have come up with is to run screen as part of the ssh command to connect to the remote server:

$ ssh -t [email protected] screen -r

or in PuTTY enter screen -r into the Remote command box of the SSH preferences.

I tend to alias a number of ssh commands to different hosts and different screen sessions.

1
  • This is what I use. Seems like a better idea than hacking .bash_profile to always run screen. Feb 6, 2015 at 13:53
1

Add this to ~/.bash_profile.

if [ $- = *i* ] && [ -z "${STY+x}" ] && which screen &>/dev/null; then
    exec screen -RR -U
fi

This will

  1. checks for interactive (only run screen when interactive, otherwise it automated programs will fail (e.g. scp, WinSCP)
  2. checks that STY is not set (if STY is set then screen is already running for this shell)
  3. check that screen is found in the PATH

And lastly run screen with option -R to resume an unattached session, if no unattached sessions then start a new session.


Start screen after importing ~/.bashrc. e.g.

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi
if [ $- = *i* ] && [ -z "${STY+x}" ] && which screen &>/dev/null; then
...
1
  • Whoops, was exec screen -R which will have problems if there is more than one detached screen! Corrected to exec screen -RR. Apr 18, 2019 at 4:55
0

Can your OS support command-line switches with the shell command definition in your "login" parameter for your account? If so, try changing it to:

  • /path/to/bash -c "screen -R"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .