如何在没有屏幕或tmux的情况下重新连接到意外断开的ssh会话

我知道这些问题已被问过多年,而答案往往是屏幕或tmux.
如果我知道我将离开会话很长一段时间,或者网络太糟糕而无法保持可靠的连接,我肯定会在开始时使用屏幕.

主要的问题是,当我开始一些会话并发现它必须持续很长时间,或者连接只是意外丢失.在后一种情况下,通常当我立即开始另一个会话时,我可以发现之前的进程没有被杀死,但我无法重新连接到他们的终端.

所以我想知道在意外断开ssh会话后很长时间内是否有可能阻止正常进程被杀死.最重要的是我可以重新连接到他们的终端,而不是提前在屏幕上启动它们.

如果没有,是否可以将已经启动的裸ssh会话移动到新的屏幕会话中以便以后重新连接?

解决方法

我不相信没有像屏幕这样的东西.一旦你的伪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.

你需要reptyr.

方案2:

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

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

ServerAliveInterval 60
ServerAliveCountMax 5

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

相关文章

linux常用进程通信方式包括管道(pipe)、有名管道(FIFO)、...
Linux性能观测工具按类别可分为系统级别和进程级别,系统级别...
本文详细介绍了curl命令基础和高级用法,包括跳过https的证书...
本文包含作者工作中常用到的一些命令,用于诊断网络、磁盘占满...
linux的平均负载表示运行态和就绪态及不可中断状态(正在io)的...
CPU上下文频繁切换会导致系统性能下降,切换分为进程切换、线...