Automatically (or more easily) reconnect to a screen session after network interruption

dreeves picture dreeves · Jan 23, 2009 · Viewed 29.8k times · Source

ADDED: This question is now, I believe, subsumed by this one: Using GNU Screen completely transparently and automatically

See also this related question:
https://superuser.com/questions/147873/ssh-sessions-in-xterms-freeze-for-many-minutes-whenever-they-disconnect

Original question:

It would be nice if there were a way to ssh to a machine and immediately reconnect to a specific screen session. You can do this:

laptop> ssh server.com screen -ls

and it will show a list of screens available on server.com like so [1]:

123.pts-1
456.pts-2

And then you might try to do this:

laptop> ssh server.com screen -dr pts-2

but that fails, saying "Must be connected to a terminal." You have to ssh in first and then do the "screen -dr pts-2" on server.com which is no good if you have a flaky connection and get disconnected a lot. You want to be able to resume with a simple "up-arrow enter" on the laptop. (Or perhaps make it even more automatic.)

I have a rihackulous solution to this problem which I'll post as an answer and hope it gets downvoted to oblivion in favor of the Right Way to deal with this.


Footnotes:

[1] Or, better, if you created the screen sessions with names like "screen -S foo" and "screen -S bar" then you'll get a friendlier list like:

123.foo
456.bar

and can reconnect with, eg, "screen -dr foo".


Mini screen tutorial, incorporating the answer to this question:

Login in to server.com and do

screen -S foo 

and then never log out of that session again. To reconnect to it from elsewhere, do

ssh -t server.com screen -dr foo

To list available screens to reconect to:

screen -ls

or, of course,

ssh server.com screen -ls

to check on server.com's available screens remotely.

I now use the following alias (tcsh), based on Jason's answer below, to connect to a named screen if it exists or create and connect otherwise:

alias ssc 'ssh -t \!:1 "screen -S \!:2 -dr || screen -S \!:2"'

Answer

Greg Hewgill picture Greg Hewgill · Jan 24, 2009

Does the -t option do what you want?

     -t      Force pseudo-tty allocation.  This can be used to execute arbi-
             trary screen-based programs on a remote machine, which can be
             very useful, e.g. when implementing menu services.  Multiple -t
             options force tty allocation, even if ssh has no local tty.

So:

laptop> ssh -t server.com screen -dr pts-2

This seems to work in my installation.