Basically I have a remote screen session, which I wish to automatically reattach to.. Currently I'm doing this by with the following command (as an iTerm bookmark, or an alias)

ssh host -t screen -x thesessionname

This works fine, but if the session dies for whatever reason, I'd like it to be recreated when I next connect. The -R flag for screen is almost perfect:

ssh host -t screen -R -S thesessionname

..but if the session is already attached, a second session gets made (as -R simple looks for the first detached session, if none are found it creates a new one)

Is there a way to make the -R flag look for attached sessions also, and only create a new one if thesessionname doesn't exist?

If this is not easily doable, how could I automatically recreate the screen session when it dies? Perhaps a script run via cron that looks for the named session, creating it should it not exist?


Tell screen to be a bit more persistent about trying:

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

So combine the two and you should have your solution ("-DR" is equivalent to "-D -R"):

screen -DR <yoursession> 

Additionally and useful to know, you can view running sessions with:

screen -ls
  • 6
    Perfect, screen -x -R -S main is what I ended up using (works the same without the forced-detach), thanks!
    – dbr
    Jul 9 '09 at 16:53
  • Sometimes you want to use sceen -r <yoursession>, will give an error when it's already attached (instead of creating a new session).
    – danger89
    Apr 4 '18 at 14:25

A few good options. First, my favorite:

screen -x -R

That will attach to the first screen session available (sharing if another connection is connected), and if there are no screen sessions at all, start a new one.

screen -d -RR

Docs for that say "Reattach a session and if necessary detach or create it. Use the first session if more than one session is available." Basically, the double-R makes it look for the first session to attach to.

screen -D -RR

"Attach here and now. Whatever that means, just do it." Will detach and remotely logout any other attached sessions and attach to the first session it can. Basically, does whatever it needs to do to attach to an existing session, and creates a new one if there's nothing there.

The very first (-x -R) is the least likely to cause problems (won't detach your other connection) but just plain always attaches to something if there's anything to attach to and creates something if there's nothing to attach to.

  • 1
    These are all nice/handy options, but none of them answer the original question, which was to (re)connect to a session by name.
    – Daniel
    Jan 26 '17 at 18:35

I use

screen -dRRS sessname

I don't want my entire login on the other shell dropped, like -D does, and I definitely don't want the multi-attach mode of -x.

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.