我希望通过 ssh 登录到远程计算机时自动启动 gnu screen,因此我添加了
exec screen
到 .bash_profile 文件的末尾,然后我发现当我终止筛选时,我与 ssh 主机的连接也会立即关闭。我该如何避免这种情况?
如果我想恢复一个屏幕(例如名为“旧屏幕”),因为我每次登录时都会开始屏幕,所以我会面临这样的情况:我已连接到“新屏幕”,而我想重新连接到“旧屏幕”。如果我只是
screen -r old-screen
我发现自己处于一个递归屏幕中,我无法在“旧屏幕”内导航,因为所有快捷键都被“新屏幕”接收。如果我尝试退出当前屏幕,我与远程计算机的连接也会立即丢失。
有没有什么解决办法?
答案1
您可以不使用screen -r
“尝试恢复屏幕会话”来尝试恢复屏幕会话,而是使用“screen -R
尝试恢复屏幕会话”来尝试恢复屏幕会话,如果不存在则创建一个新的会话。
-r [pid.tty.host]
-r sessionowner/[pid.tty.host]
resumes a detached screen session. No other options (except com-
binations with -d/-D) may be specified, though an optional prefix
of [pid.]tty.host may be needed to distinguish between multiple
detached screen sessions. The second form is used to connect to
another user's screen session which runs in multiuser mode. This
indicates that screen should look for sessions in another user's
directory. This requires setuid-root.
-R attempts to resume the first detached screen session it finds. If
successful, all other command-line options are ignored. If no
detached session exists, starts a new session using the specified
options, just as if -R had not been specified. The option is set
by default if screen is run as a login-shell (actually screen uses
"-xRR" in that case). For combinations with the -d/-D option see
there.
我个人倾向于使用screen -DRA
。
-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.
-A Adapt the sizes of all windows to the size of the current termi-
nal. By default, screen tries to restore its old window sizes
when attaching to resizable terminals (those with "WS" in its
description, e.g. suncmd or some xterm).
答案2
以下内容似乎对我有用:〜/ .bash_profile
## 如果未设置 $STY... 如果 [ -z "$STY" ]; 那么 屏幕-d-RR 菲
我从这里得到答案: https://superuser.com/a/52329/76204
但它不是每次重新连接会话时都创建新屏幕,或者在必要时创建一个屏幕。此外,似乎在重新连接或关闭屏幕时,两个 ssh 连接都不会退出。
答案3
我自己也经常思考同样的问题。
我想到最接近的方法是运行 screen 作为 ssh 命令的一部分来连接到远程服务器:
$ ssh -t [email protected] screen -r
或者在 PuTTY 中输入screen -r
SSHRemote command
首选项框。
我倾向于将多个 ssh 命令别名为不同的主机和不同的屏幕会话。
答案4
您的操作系统是否支持在帐户的“登录”参数中使用 shell 命令定义的命令行开关?如果是,请尝试将其更改为:
- /路径/到/bash-c“屏幕-R”