当没有 运行 会话时,Tmux 无法连接到 `tmux ls` 上的服务器错误

Tmux failed to connect to server error on `tmux ls` when there are no running sessions

只是编写一个快速循环来在我登录服务器时列出现有的 tmux 会话,这取决于是否安装了 tmux(通过 CentOS 上的 .bashrc)。

if rpm -q tmux; then
    echo -e "TMUX sessions running:\n"
    echo `tmux ls`
fi

这在 tmux 有一个或两个会话时效果很好,但如果没有 运行 个 tmux 会话,我会得到 failed to connect to server: No such file or directory

有没有办法抑制这个?

谢谢!

结合使用@Barmar 和@Etan Reisner 的建议:

tmux ls 2> /dev/null

没有会话时不回显,否则报列表

请注意,您可能有一个 tmux 服务器 运行,但您无法连接到它,因为有人清除了 /tmp 目录并带走了服务器的套接字。

在这种情况下,您可以通过向服务器发送 SIGUSR1 信号来告诉服务器重新创建套接字。

% ps aux | grep -w [t]mux
root     14799  0.2  0.0  36020   488 ?        Ss   May08  51:30 tmux
% kill -USR1 14799
% tmux ls
<list of tmux sessions>