5

Just writing a quick loop to list out existing tmux sessions when I log into a server, depending on whether tmux is installed (via .bashrc on CentOS).

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

This works great when tmux has a session or two, but if there are no running tmux sessions, I'm getting failed to connect to server: No such file or directory.

Is there a way to suppress this?

Thanks!

economy
  • 4,035
  • 6
  • 29
  • 37
  • 5
    Redirect `stderr` to `/dev/null` – Barmar Apr 24 '15 at 19:13
  • 4
    There is no reason for the echo on that `tmux ls` line. It is entirely pointless (and actively harmful of the `ls` output's spacing). – Etan Reisner Apr 24 '15 at 20:20
  • 1
    Both of your comments were helpful, and I have the output I wanted. Still unclear why tmux would output an error in the presence of no sessions, though. – economy Apr 24 '15 at 21:23

2 Answers2

29

Note that you may have a tmux server running, but you cannot connect to it because someone cleaned out the /tmp directory and took the server's socket with it.

In that case, you can tell the server to recreate the socket by sending it a SIGUSR1 signal.

% 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>
dland
  • 4,319
  • 6
  • 36
  • 60
4

Using a combination of @Barmar and @Etan Reisner 's advice:

tmux ls 2> /dev/null

Nothing is echoed in when there are no sessions, otherwise the list is reported.

economy
  • 4,035
  • 6
  • 29
  • 37