网络中断后自动(或更容易)重新连接到屏幕会话 [英] Automatically (or more easily) reconnect to a screen session after network interruption
问题描述
添加:我认为,这个问题现在包含在这个问题中: 完全透明,自动地使用GNU屏幕
ADDED: This question is now, I believe, subsumed by this one: Using GNU Screen completely transparently and automatically
另请参阅以下相关问题:
https://superuser.com/questions/147873/ssh-sessions-in-xterms在每次断开连接时冻结很多分钟
See also this related question:
https://superuser.com/questions/147873/ssh-sessions-in-xterms-freeze-for-many-minutes-whenever-they-disconnect
原始问题:
如果有一种方法可以SSH到计算机并立即重新连接到特定的屏幕会话,那就太好了.您可以这样做:
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
,它将显示server.com上可用的屏幕列表,如下所示:[1]:
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
但是失败,说必须连接到终端". 您必须先使用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.
脚注:
[1]或者,更好的是,如果您使用"screen -S foo
"和"screen -S bar
"之类的名称创建了屏幕会话,那么您将获得一个更友好的列表,例如:
[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
,并且可以与"screen -dr foo
"重新连接.
and can reconnect with, eg, "screen -dr foo
".
登录server.com并执行
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
,当然,
ssh server.com screen -ls
以远程查看server.com的可用屏幕.
to check on server.com's available screens remotely.
我现在根据以下杰森的回答,使用以下别名(tcsh)连接到命名屏幕(如果存在),否则创建并连接:
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"'
推荐答案
-t
选项是否可以满足您的要求?
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.
所以:
laptop> ssh -t server.com screen -dr pts-2
这似乎在我的安装中有效.
This seems to work in my installation.
这篇关于网络中断后自动(或更容易)重新连接到屏幕会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!