Reattaching to an existing screen session

365,545

Solution 1

There are two levels of "listings" involved here. First, you have the "window listing" within an individual session, which is what ctrl-A A is for, and second there is a "session listing" which is what you have pasted in your question and what can also be viewed with screen -ls.

You can customize the session names with the -S parameter, otherwise it uses your hostname (teamviggy), for example:

$ screen

(ctrl-A d to detach)

$ screen -S myprogramrunningunderscreen

(ctrl-A d to detach)

$ screen -ls

There are screens on:
    4964.myprogramrunningunderscreen    (05/31/2013 09:42:29 PM)    (Detached)
    4874.pts-1.creeper  (05/31/2013 09:39:12 PM)    (Detached)
2 Sockets in /var/run/screen/S-paul.

As a bonus, you can use an unambiguous abbreviation of the name you pass to -S later to reconnect:

screen -r myprog

(I am reconnected to the myprogramrunningunderscreen session)

Solution 2

I had a case where screen -r failed to reattach. Adding the -d flag so it looked like this

screen -d -r

worked for me. It detached the previous screen and allowed me to reattach. See the Man Page for more information.

Solution 3

An easy way is to simply reconnect to an arbitrary screen with

screen -r

Then once you are running screen, you can get a list of all active screens by hitting Ctrl-A " (i.e. control-A followed by a double quote). Then you can just select the active screens one at a time and see what they are running. Naming the screens will, of course, make it easier to identify the right one.

Just my two cents

Solution 4

I tend to use the following combo where I need to work on several machines in several clusters:

screen -S clusterX

This creates the new screen session where I can build up the environment.

screen -dRR clusterX

This is what I use subsequently to reattach to that screen session. The nifty bits are that if the session is attached elsewhere, it detaches that other display. Moreover, if there is no session for some quirky reason, like someone rebooted my server without me knowing, it creates one. Finally. if multiple sessions exist, it uses the first one.

Much kudos to https://support.dvsus.com/hc/en-us/articles/212925186-Linux-GNU-Screen-instructions for this tip a while back.

EDIT:

Also here's few useful explanations from man screen on cryptic parameters

       -d -r   Reattach a session and if necessary detach it first.

       -d -R   Reattach a session and if necessary detach or  even  create  it
               first.

       -d -RR  Reattach  a  session  and if necessary detach or create it. Use
               the first session if more than one session is available.

       -D -r   Reattach a session. If necessary  detach  and  logout  remotely
               first.

there is more with -D so be sure to check man screen

Solution 5

The output of screen -list is formatted like pid.tty.host. The pids can be used to get the first child process with pstree:

screen -list|cut -f1 -d'.'|cut -f2|xargs -n 1 pstree -p|grep "^screen"

You will get a list like this

screen(5169)---zsh(5170)---less(15268)
screen(4872)---zsh(4873)-+-cat(11364)
...
Share:
365,545

Related videos on Youtube

JohnMerlino
Author by

JohnMerlino

Looking to master Trigonometry and Calculus and an interest in Ruby and JavaScript programming languages. I only use Linux (in particular Ubuntu Desktop) and Android. I like to write as well.

Updated on September 18, 2022

Comments

  • JohnMerlino
    JohnMerlino over 1 year

    I have a program running under screen. In fact, when I detach from the session and check netstat, I can see the program is still running (which is what I want):

    udp        0      0 127.0.0.1:1720          0.0.0.0:*                           3759/ruby       
    

    Now I want to reattach to the session running that process. So I start up a new terminal, and type screen -r

    $ screen -r
    There are several suitable screens on:
        5169.pts-2.teamviggy    (05/31/2013 09:30:28 PM)    (Detached)
        4872.pts-2.teamviggy    (05/31/2013 09:25:30 PM)    (Detached)
        4572.pts-2.teamviggy    (05/31/2013 09:07:17 PM)    (Detached)
        4073.pts-2.teamviggy    (05/31/2013 08:50:54 PM)    (Detached)
        3600.pts-2.teamviggy    (05/31/2013 08:40:14 PM)    (Detached)
    Type "screen [-d] -r [pid.]tty.host" to resume one of them.
    

    But how do I know which one is the session running that process I created?

    Now one of the documents I came across said:

    "When you're using a window, type C-a A to give it a name. This name will be used in the window listing, and will help you remember what you're doing in each window when you start using a lot of windows."

    The thing is when I am in a new screen session, I try to press control+a A and nothing happens.

    • Admin
      Admin almost 7 years
      A fast way to select one from a list of multiple open screens, is screen -r followed by a number 1, 2, 3 ... in that list of screens.
    • Admin
      Admin about 2 years
      With modern versions of screen, it is possible to rename the sessionname from within a started screen session: type ctrl-a + : followed by sessionname <your chosen name>
  • JohnMerlino
    JohnMerlino almost 11 years
    That ctrl+a a works for you? I press control + a at same time and then quickly press a again and nothing happens.
  • Michael
    Michael over 8 years
    I do: "screen -r 21tweet" and get as reponse: "There is a screen on: 3239.21tweet (08/21/2015 09:14:35 AM) (Attached) There is no screen to be resumed matching 21tweet." What could this be?
  • Spencer Williams
    Spencer Williams over 7 years
    A very not-obvious caveat with naming screen sessions is that if you name them with -S, you have to use at the very least an unambiguous abbreviation of the name following the ID number and dot in the screen session's full name when resuming, unlike the automatically generated session names, where you can include the ID and dot.
  • Admin
    Admin about 7 years
    "ctrl-A to detach" Huh?
  • Paul
    Paul about 7 years
    Typo, fixed. And this site will not let me post a comment that short without some additional text.
  • austin
    austin over 6 years
    okie how i name my screen?
  • notbrain
    notbrain about 6 years
    @austin screen -S <sessionname>
  • Josh
    Josh about 4 years
    This one much easier ^
  • Jeremy Davis
    Jeremy Davis about 3 years
    IMO you should note what the '-d' switch does... FYI '-d' detaches an "attached" session (i.e. an existing session attached somewhere else), and '-r' reattaches locally.
  • Admin
    Admin almost 2 years
    To extend on this answer a bit - when you do a screen -list and see its in (Attached) mode this is the right command. It happens if you have a spotty connection. One of my main reasons for screen.