Linux 当我尝试列出会话时,为什么会从 tmux 收到“无法连接到服务器”的消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9668763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:07:33  来源:igfitidea点击:

Why am I getting a "failed to connect to server" message from tmux when I try to list sessions?

linuxsshterminaltmux

提问by thinke365

Here's what's happening to me: I start tmux sessions using tmux -L name1, tmux -L name2; then I detatch them using ctrl+B+d. Then I try to get a list of the currently running sessions on my computer. However, when I run tmux ls, I get an error message:

这是发生在我身上的事情:我使用tmux -L name1,开始 tmux 会话tmux -L name2;然后我用detatch他们ctrl+ B+ d。然后我尝试获取计算机上当前正在运行的会话的列表。但是,当我运行时tmux ls,我收到一条错误消息:

failed to connect to server: Connection refused

Is this a bug? I'm familiar with screen; I regard screen -lsas a very useful function since I might start a session and leave it running for weeks before the next time I attach to it. Because of this, the ability to list current running tmux sessions is quite important for me. Why does tmux lsreturn a "connection refused" error when I know tmux is running?

这是一个错误吗?我熟悉屏幕;我认为这screen -ls是一个非常有用的功能,因为我可能会启动一个会话并在下一次附加到它之前让它运行数周。因此,列出当前正在运行的 tmux 会话的能力对我来说非常重要。tmux ls当我知道 tmux 正在运行时,为什么会返回“连接被拒绝”错误?

采纳答案by Lars Kotthoff

Try tmux -L name1 list-session.

试试tmux -L name1 list-session

回答by Rose Perrone

You may have an error in your .tmux.conf. I had this problem until i took out this line from my .tmux.conf:

您的.tmux.conf. 我遇到了这个问题,直到我从我的.tmux.conf

set-window-option -g xterm-keys on

You could also try tmux -vand then look at the logs it prints.

您也可以尝试tmux -v然后查看它打印的日志。

回答by Nathan

This happens to me when I do not have any sessions running. I'm just starting to use tmux and didn't realize that if you restart your computer you lose your sessions which surprised me at first.

当我没有任何会话运行时,就会发生这种情况。我刚刚开始使用 tmux,并没有意识到如果您重新启动计算机,您将丢失会话,这让我一开始感到惊讶。

For those of you who are thinking the same thing: Restore tmux session after reboot. A summary of the post: Use shell scripts to build your tmux sessions or create a fancy shell history tracker.

对于那些有同样想法的人:在重启后恢复 tmux 会话。文章摘要:使用 shell 脚本构建您的 tmux 会话或创建一个漂亮的shell 历史跟踪器

回答by akofink

I was using another program inside of tmux (reattach-to-user-namespace), and I was getting this error when I switched computers because reattach-to-user-namespace was not installed. The fix was to simply run brew install reattach-to-user-namespace.

我在 tmux(重新附加到用户命名空间)中使用了另一个程序,当我切换计算机时出现此错误,因为未安装重新附加到用户命名空间。解决方法是简单地运行brew install reattach-to-user-namespace.

回答by gitaarik

You get this error indeed if there are no session open. If there are no sessions open there is no tmux server running so it can't connect to it.

如果没有会话打开,您确实会收到此错误。如果没有会话打开,则没有 tmux 服务器在运行,因此无法连接到它。

With the -Loption, you change the socket name the tmux server uses, it's not a way to name your sessions. You better use the following commands:

使用该-L选项,您可以更改 tmux 服务器使用的套接字名称,这不是一种命名会话的方法。您最好使用以下命令:

tmux new -s name1
tmux new -s name2

These will create 2 sessions on a server with the default socket name. Now you can do:

这些将在具有默认套接字名称的服务器上创建 2 个会话。现在你可以这样做:

$ tmux ls
name1: 1 windows (created Mon Sep 22 10:34:40 2014) [158x40] (attached)
name2: 1 windows (created Mon Sep 22 10:34:43 2014) [158x40] (attached)

And you see all the sessions running on the server on the default socket. You can reattach one of them using:

您会在默认套接字上看到服务器上运行的所有会话。您可以使用以下方法重新附加其中之一:

tmux attach -d -s name1

-sspecifies the name of the session
-dwill detach it from it's previous client (if it is attached)

-s指定会话的名称
-d将其与之前的客户端分离(如果已附加)

You can also switch between sessions inside tmux with the choose-treecommand which by default is assigned to the keystroke C-s(prefix key + s). This is what I usually do.

您还可以使用choose-tree默认分配给击键C-s(前缀键 + s)的命令 在 tmux 内的会话之间切换。这是我通常做的。

回答by Shumin Guo

One easy fix is to delete the tmp files left by the tmux server, for example, by doing $ rm -rf /tmp/tmux-xxx/.

一种简单的解决方法是删除 tmux 服务器留下的 tmp 文件,例如,执行$ rm -rf /tmp/tmux-xxx/.

回答by 7heo.tk

The way TMUX(1)works is by having a client process (tmux) connect to a server process (tmuxtoo, but not attached to a TTY), as shown in the following psoutput:

TMUX(1)工作方式是让客户端进程 ( tmux) 连接到服务器进程(tmux也连接到服务器进程,但不附加到 TTY),如以下ps输出所示:

  PID TTY      STAT   TIME COMMAND
19229 pts/1    S+     0:00 tmux
19231 ?        Ss     0:00 tmux

That shows that the client actually starts before the server (one could assume it forks it).

这表明客户端实际上在服务器之前启动(可以假设它分叉了它)。



After detach/re-attach, the same pscommand outputs:

分离/重新连接后,相同的ps命令输出:

  PID TTY      STAT   TIME COMMAND
19231 ?        Ss     0:00 tmux
19290 pts/1    S+     0:00 tmux attach

This shows the tmux client as tmux attach, thus being a bit easier to understand.

这将 tmux 客户端显示为tmux attach,因此更容易理解。



Now, if we look at the output of pstreein both of the above cases, we get in both cases (ignoring the pidchange for tmux attach):

现在,如果我们查看pstree上述两种情况下的输出,我们会得到两种情况(忽略 的pid变化tmux attach):

pstree -p
init(1)─┬─acpid(1824)
        ├─cron(1859)
        ?
        ├─sh(14146)───tmux(19229)
        └─tmux(19231)───sh(19233)───pstree(19234)

Clearly showing that the commands typed (pstreein this case) in the client process (PID 19229) get executed by the server one (PID 19231), thus allowing them to continue without SIGHUPin the event where the client terminal is lost (over ssh, for example).

清楚地表明pstree在客户端进程 (PID 19229) 中键入的命令 (在这种情况下)由服务器 (PID 19231) 执行,从而允许它们在客户端丢失的情况下在没有SIGHUP的情况下继续(例如通过 ssh) )。



Now, to the question OP asked: what happens in the case where tmuxreturns failed to connect to server: Connection refusedis that the server process (pid 19231 in our case) is unreachable, whatever the reason (it can be because the server process died; but also because the user executing the tmuxclient doesn't have the permissions to access the tmux socket, etc.)

现在,对于 OP 提出的问题:在tmux返回的情况下会发生什么情况failed to connect to server: Connection refused是服务器进程(在我们的例子中为 pid 19231)无法访问,无论是什么原因(可能是因为服务器进程死了;但也因为用户执行tmux客户端无权访问 tmux 套接字等)

The solution in that case is to grepfor the tmuxprocesses (via psfor example), and pray that you didn't get this error because the server died (so you can attach to it by using lsofto get what sockets it listens to). Otherwise, there is no way to attachto the server, as it is as dead as after a reboot.

在这种情况下的解决方案是grep针对tmux进程(ps例如通过),并祈祷您没有因为服务器死亡而收到此错误(因此您可以通过使用它lsof来获取它侦听的套接字来附加到它)。否则,无法连接到服务器,因为它就像重新启动后一样死机。



TL;DR:

特尔;博士:

This error can be given for multiple reasons, ranging from bug to critical failure (program died). In a nutshell, use the UNIX tools at your disposal to determine what socket does tmuxuse, if it is still running (there should be at least two processes if you have the tmux client running - that happens after invoking tmuxor tmux attachfrom the shell) and thus if you lost your session or not.

这个错误可能有多种原因,从错误到严重失败(程序死亡)。简而言之,使用您可以使用的 UNIX 工具来确定tmux使用的套接字是否仍在运行(如果您运行 tmux 客户端,则至少应该有两个进程 - 在调用tmuxtmux attach从 shell调用之后发生),因此如果您丢失了会话。

Note: as other answers pointed out, if the reason for this error to be shown is a socket error, you can use the -Lflag to tell tmuxto use a specific socket.

注意:正如其他答案所指出的,如果显示此错误的原因是套接字错误,则可以使用该-L标志来告诉tmux使用特定的套接字。

回答by Abe Voelker

This happened to me when the Ubuntu desktop crashed and my gnome-terminal windows exited. I could still see the tmux process was running (ps aux | grep tmux) but for some reason tmux commands would not work to list the existing sessions. Apparently it wasn't finding the existing Unix socket of the still-running tmux process. The fix in this scenario is to locate the existing Unix socket and specify that to tmux using the -Sflag; here's how:

当 Ubuntu 桌面崩溃并且我的 gnome 终端窗口退出时,这发生在我身上。我仍然可以看到 tmux 进程正在运行 ( ps aux | grep tmux) 但由于某种原因 tmux 命令无法列出现有会话。显然它没有找到仍在运行的 tmux 进程的现有 Unix 套接字。这种情况下的修复是定位现有的 Unix 套接字并使用-S标志将其指定给 tmux ;就是这样:

You can find the PID of your still-running tmux process with this:

您可以使用以下命令找到仍在运行的 tmux 进程的 PID:

ps -p $(pidof tmux)

Now take your PID (in my case, 6876) and run this to list any open Unix sockets:

现在获取您的 PID(在我的例子中是 6876)并运行它以列出所有打开的 Unix 套接字:

sudo lsof -Uap 6876

Hopefully you see output like this:

希望你看到这样的输出:

COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF   NODE NAME
tmux    6876  abe    3u  unix 0x0000000000000000      0t0 408477 socket
tmux    6876  abe    4u  unix 0x0000000000000000      0t0 408478 socket
tmux    6876  abe    6u  unix 0x0000000000000000      0t0 408479 /tmp/tmux-1000/default

Now you can specify that existing Unix socket to your tmux command (using the -Sflag), and you should be able to list-sessions and attach properly:

现在您可以将现有的 Unix 套接字指定到您的 tmux 命令(使用该-S标志),并且您应该能够列出会话并正确附加:

tmux -S /tmp/tmux-1000/default list-sessions
tmux -S /tmp/tmux-1000/default attach -t 0

回答by Pierre Maoui

This may happen if you or any cleaning process delete files in /tmp/*. All your sessions data are lost if you can't recover those files. Killing all tmux instances and restart it is the only choice left, unfortunately.

如果您或任何清理过程删除/tmp/*. 如果您无法恢复这些文件,您的所有会话数据都会丢失。不幸的是,杀死所有 tmux 实例并重新启动它是唯一的选择。

回答by haridsv

TL;DR:Try sending SIGUSR1signal to the tmux server process.

TL;DR:尝试向SIGUSR1tmux 服务器进程发送信号。

In my case, after about 8 days of inactivity, I was not able to reattach:

就我而言,在大约 8 天不活动后,我无法重新连接:

$ tmux attach
no sessions

However, a grep for tmux process got me this output:

但是,tmux 进程的 grep 得到了这个输出:

$ ps -aef | fgrep -i tmux
hari     7139     1  1  2016 ?        2-20:32:31 tmux
hari    25943 25113  0 22:00 pts/0    00:00:00 fgrep --color=auto -i tmux

As suggested by @7heo.tk, this indicates that tmux server is still running, but tmux lswas giving failed to connect to server: Connection refusederror. I verified that the tmp directory that belonged to the tmux session existed and lsof -p 7139(the pid of tmux server) showed that the socket file is open:

正如@ 7heo.tk建议,这表明TMUX服务器仍在运行,不过tmux ls是给failed to connect to server: Connection refused错误。我验证了属于 tmux 会话的 tmp 目录存在并且lsof -p 7139(tmux 服务器的 pid)显示套接字文件已打开:

COMMAND  PID  USER   FD   TYPE             DEVICE SIZE/OFF       NODE NAME
tmux    7139 hari    5u  unix 0x0000000000000000      0t0 1712879255 /tmp/tmux-50440/default

I also tried explicitly specifying the -S /tmp/tmux-50440/defaultto tmux but it didn't help. However, I read in the tmux man page that sending SIGUSR1would make tmux recreate the socket file, so I tried that and I was able to immediately find the session and reattach:

我还尝试明确指定-S /tmp/tmux-50440/defaultto tmux ,但没有帮助。但是,我在 tmux 手册页中读到发送SIGUSR1将使 tmux 重新创建套接字文件,因此我尝试了该操作,并且能够立即找到会话并重新附加:

$ kill -s USR1 7139
$ tmux ls
0: 12 windows (created Mon Apr 18 21:17:55 2016) [198x62]