2013-11-22 51 views
4

我知道这些类型的问题已被问了多年,其答案往往是screentmux。 我一开始会使用screen,如果我知道我会离开会话很长一段时间,或者网络太糟糕而无法保持可靠的连接。如何重新连接到意外断开ssh会话无屏幕或tmux

主要问题是当我开始某个会话并发现它必须持续很长时间,或者连接刚刚意外丢失。在后一种情况下,通常当我立即开始另一个会话时,我可以发现以前的进程不会在那个时候被终止,但我无法重新连接到它们的终端。

所以我想知道是否有可能阻止正常进程在意外断开ssh会话后很长一段时间内死亡。而最重要的是,我可以重新连接到他们的终端与开始他们提前screen

如果没有,是否可以将已启动的裸ssh会话移动到新的screen会话中以供稍后重新连接?

+0

出于好奇,为什么你不想调用屏幕? – SpliFF

+1

@SpliFF通常不是我不想,但仅仅是因为我在问题中说过了太迟了(意外断开或意识到我需要在中途会话中使用屏幕)。顺便说一句,屏幕也打破腻子和iterm的滚动回滚,允许通过拖动滚动条轻松滚动,也许它可以调整?但我懒得解决这个问题。 – lyu

回答

1

我不相信有没有像屏幕的东西是可能的。一旦你的伪TTY丢失了,我几乎可以确定它不能从一个不同的shell中恢复(至少不是没有一些简单的黑客攻击)。

只要添加一个现有的过程到一个新的屏幕,我认为这是可能的。这里尝试说明:http://monkeypatch.me/blog/move-a-running-process-to-a-new-screen-shell.html

The first thing to do is to suspend the process. In my case, Irssi can be suspended by typing ctrl+z. 

Secondly, resume the process in background: 

$ bg 

Now, we will detach the process from its parent (the shell). So, when the parent process will be terminated, the child (Irssi) will be able to continue. For this, we use the disown builtin: 

$ disown irssi 

Launch a screen session: 

$ screen 

As we are in a screen session, we will retrieve the irssi process. To do so, we use the reptyr command which take a pid: 

$ reptyr <pid> 

To avoid the tedious pid research, we can use the pgrep command: 

$ reptyr $(pgrep irssi) 

Now the process is in a screen shell, we can safely detach our session and no longer worry about killing our X server or close our ssh connection. 

You'l需要reptyr这一点。

选项2:

我怀疑你可能试图解决错误的问题。如果您的SSH连接正在丢失,为什么不解决该问题?您可以通过调整连接设置来将SSH设置为非常容忍超时和断开连接。

在您的客户端,在$HOME/.ssh/config加:

ServerAliveInterval 60 
ServerAliveCountMax 5 

现在,即使服务器没有5分钟响应您的会话将不会超时。

+0

我不太了解OPTION 2,我知道可以设置超时时间,但是一旦我失去了连接,是不是全部消失了?即使超时是inf,我仍然无法重新连接到它? – lyu

+1

选项2是关于最大限度地减少首先丢失连接的机会。即使您的互联网连接4分钟,上述选项也会让您的会话继续进行。但是,是的,一旦它真的走了(像客户端重新启动,意外关闭终端等),那就是非常多。 – SpliFF

+0

谢谢,我现在得到了选项2。但选项1有一些问题: – lyu

0

使用ssh-TMUX代替TMUX:

function ssh-tmux(){ 
    if ! command -v autossh &> /dev/null; then echo "Install autossh"; fi 
    autossh -M 0 $* -t 'byobu || {echo "Install byobu-tmux on server..."} && bash' 
} 
0

我的工作用纳米文本文件和我断开了。登录后,我看到以前会话的nano进程仍在运行,但我无法切换到该nano实例。所以,我杀了纳米进程,然后创建了一个名为:filename.save的文件,这个文件从第一个会话中进行了更改。

相关问题