After starting multiple gnu screen sessions with

screen -S name1
screen -S name2

I would like to selectively attach to them. If I do

screen -D -R name1

it will not attach but instead print:

There are several suitable screens on:
    6799.name1        (08.02.2013 15:47:23)   (Detached)
    3661.name2        (08.02.2013 13:43:25)   (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.

It works obviously if I type:

screen -D -R 6799.name1

But I would like to attach without knowing the tty number, because I want to start the screen sessions by a script which takes the name as argument. Not knowing the tty number at this time I would need to copy by hand this number whenever I want to reattach later.

An obvious solution would be to extract the tty number from the above screen output using string operations. But that seems a little awkward. Is there a way to tell screen to attach to a specific named session, whatever the tty number is?

Note that I tried the different [-d|-D] [-r|-R] combinations mentioned in the man pages.

Note added:

I create the separate screen sessions in a script using ssh with

ssh -t ${host} "
    screen -S \"$exefile\" -t \"$exefile\" -R "nice -n$prio \"$exefile\" ${exeparams[@]}"; 
    exit 0";

Later I try to connect using

ssh -t ${host} "
    screen -D -R -S \"$exefile\" && 
    echo \"finished.\"; 

where $exefile is the name of the executable and the screen session I want to connect to.

  • I actually went for the awkward solution when I wrote a wrapper shell script around screen github.com/frostschutz/Screen-Manager - it has a sm_pidof() function that extracts the number for a given name. But it also needs that for other check (such as is the screen already running or what). Commented Feb 8, 2013 at 15:59

3 Answers 3


Because I am the only one connecting to my screen sessions, and sometimes I want more than one console connected (a la multi display mode) to a session at a time, I use the -x switch.

# screen -S hosts
# screen -ls
  There is a screen on:
      12828.hosts     (Detached)
  1 Socket in /var/run/screen/S-uther.
# screen -x hosts

From man 1 screen

   -x   Attach  to a not detached screen session. 
       (Multi display mode).Screen refuses to attach 
       from within itself.  But when cascading multiple 
       screens, loops are  not  detected; take care.
  • This could be a solution, but usually there is too much output on my screen sessions for this to be practicable.
    – highsciguy
    Commented Feb 10, 2013 at 20:02

This should work:

screen -S name1 -D -R
  • Seems I accepted this answer too early. It worked at first, but it seems to depend on the tty numbers. Now it does not work, independently of whether the screen sessions are attached, detached etc.
    – highsciguy
    Commented Feb 10, 2013 at 20:01
  • @highsciguy, what do you mean by it seems to depend on the tty numbers? I can't seem to be able to make in not work. Can you please tell how to reproduce? Commented Feb 10, 2013 at 20:06
  • Please see my comment on how I create the screen sessions. I am not sure what causes the problems but it seems to work only sometimes.
    – highsciguy
    Commented Feb 11, 2013 at 9:12
  • @highsciguy, in which way does it not work? Does it fail to attach the session, fail to detach from the other terminal? Attach the wrong session? Does it report an error? What version of screen (I'm testing with the 4.1.0~20120320gitdb59704 package from Debian) Commented Feb 11, 2013 at 10:57
  • It will print a message of the sort mentioned above "There are several suitable screens on: ...". Screen version 4.00.03jw4 (FAU) 2-May-06 (current Ubuntu).
    – highsciguy
    Commented Feb 11, 2013 at 12:19

The only reliable solution I found was the brute force method outlined in the question. To connect to an existing screen with name $exefile on remote $host via ssh, or to create a new one if it does not exist, one may use (in a bash shell):

ssh -t $host "
    echo \"done.\" &&
    echo \"itentifying screen ...\" &&
    myscreen=\`screen -list | sed -n \"s/^\\s*\\s\\([0-9]*\\.$exefile\\)\\s\\s*.*$/\\1/p\"\` &&
    if [ "X\$myscreen" = "X" ]; then 
        echo \"no existing screen session found, creating new\"; 
        screen -S \"$exefile\"; 
        echo \"connecting to screen \$myscreen\"; 
        screen -d -r \"\$myscreen\"; 
    fi &&
    echo \"finished.\";

The first run will create a new session on $host. If the screen is detached with Ctrl+a,d the ssh login shell closes. If the same command is issued again it will reconnect to the existing screen. Multiple screen sessions with different names $exefile are possible and can be reattached independently. The magic is done by sed which searches the output of screen -list for a name matching the pattern [tty].name. Note that the screen name might need to be escaped if it contains sed special characters.

You must log in to answer this question.

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