2

Sometimes the SSH shell featuring a active screen session to my remote server gets broken (internet line not very stable, for example) with the session still attached.

Then I SSH again into the server, and try to resume the screen session:

luis@Zarzamoro:~$ screen -r
There is a screen on:
        9166.pts-2.Zarzamoro    (12/23/15 23:47:34)     (Attached)
There is no screen to be resumed.
luis@Zarzamoro:~$ screen -r 9166.pts-2.Zarzamoro
There is a screen on:
        9166.pts-2.Zarzamoro    (12/23/15 23:47:34)     (Attached)
There is no screen to be resumed matching 9166.pts-2.Zarzamoro.

I don't know much about GNU screen. Why does this happen?
Is it possible to resume this screen session?

  • If you always only use one screen, make an alias rs='screen -R -D'. – ott-- Dec 24 '15 at 19:41
4

Use:

screen -r -d <pid>.<tty>.<host>

In your case:

screen -r -d 9166.pts-2.Zarzamoro

This will force detach the screen on the other end and attach.

  • What about permissions? I suppose they must work for screen sessions as they work for files, correct? – Sopalajo de Arrierez Dec 24 '15 at 2:03
3

Two points:

  • You do not have to specify the whole pid+session name (screen accepts a reasonable abbreviation).

  • You may get better results with the -x option.

From the manual page:

-r
sessionowner/[pid.sessionname]’ Resume a detached screen session.

-x
Attach to a session which is already attached elsewhere (multi-display mode). Screen refuses to attach from within itself. But when cascading multiple screens, loops are not detected; take care.

  • 2
    As an aside, the most convenient way I've used (especially when just using a single session at any given time) is to name the session when creating it, then it's easy to remember/automate. screen -S sessionname will create the session with a specific name, and then you can use screen -x sessionname to re-attach the session. – Jason Rush Dec 23 '15 at 23:44
  • Could you please expand on that "reasonable abbreviation"? – Sopalajo de Arrierez Dec 24 '15 at 2:01
  • 1
    I don't see it in the manpage, but know that screen matches the screen-name as a string, and doesn't require you to type (or paste) the entire string. Even just the first few characters of the pid-value should work, if it is unambiguous. (I have occasion to use this on a system where there may be a half-dozen screen sessions). – Thomas Dickey Dec 24 '15 at 2:14
1

This happens to me occasionally as well. What I believe happens is that your tty is not properly detached from screen and therefore when you reconnect to the server you are still "attached" to the screen.

Here's what I do:

screen -d
screen -r (if you have only one screen)
screen -r <pid> (if you have more than one screen)

You can also do

screen -r -d (add <pid> if you have more than one screen)

to detach and reattach the improperly attached screen.

Also, when doing screen -r you needn't specify the entire screen's name, only the pid (four digit number).

  • screen -d to detach all attached screen sessions? Only for the most recent? For my user? For any user? – Sopalajo de Arrierez Dec 24 '15 at 9:13
  • I believe it detaches your session from its current improperly "attached" screen. In your case, it would detach you from your pseudo-screen 9166.pts-2.Zarzamoro. – perhapsmaybeharry Dec 24 '15 at 9:57
  • 1
    According to the man page, you can even do screen -d -r. – Sopalajo de Arrierez Dec 24 '15 at 13:25
  • Yes, you can. I thought it'd be a little clearer if I indicated what each option did, such as how there was no requirement to indicate the pid if you had only one screen. I'll edit my answer to indicate this as well. – perhapsmaybeharry Dec 24 '15 at 18:56
0

You can even automate this, as shown in the here to where it will automatically reconnect to the existing screen session (or create a new screen session if none exists):

.bashrc

Add these lines at the top of ~/.bashrc on the target host:

# Auto-screen invocation. see: http://taint.org/wk/RemoteLoginAutoScreen
# if we're coming from a remote SSH connection, in an interactive session
# then automatically put us into a screen(1) session.   Only try once
# -- if $STARTED_SCREEN is set, don't try it again, to avoid looping
# if screen fails for some reason.
if [ "$PS1" != "" -a "${STARTED_SCREEN:-x}" = x -a "${SSH_TTY:-x}" != x ]
then
  STARTED_SCREEN=1 ; export STARTED_SCREEN
  [ -d $HOME/lib/screen-logs ] || mkdir -p $HOME/lib/screen-logs
  sleep 1
  screen -RR && exit 0
  # normally, execution of this rc script ends here...
  echo "Screen failed! continuing with normal bash startup"
fi
# [end of auto-screen snippet]

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.