The way TMUX(1)
works is by having a client process (tmux
) connect to a server process (tmux
too, but not attached to a TTY), as shown in the following ps
output:
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 ps
command outputs:
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.
Now, if we look at the output of pstree
in both of the above cases, we get in both cases (ignoring the pid
change for 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 (pstree
in this case) in the client process (PID 19229
) get executed by the server one (PID 19231
), thus allowing them to continue without SIGHUP in the event where the client terminal is lost (over ssh, for example).
Now, to the question OP asked: what happens in the case where tmux
returns failed to connect to server: Connection refused
is 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 tmux
client doesn't have the permissions to access the tmux socket, etc.)
The solution in that case is to grep
for the tmux
processes (via ps
for example), and pray that you didn't get this error because the server died (so you can attach to it by using lsof
to get what sockets it listens to). Otherwise, there is no way to attach to the server, as it is as dead as after a reboot.
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 tmux
use, if it is still running (there should be at least two processes if you have the tmux client running - that happens after invoking tmux
or tmux attach
from the shell) and thus if you lost your session or not.
Note: as other answers pointed out, if the reason for this error to be shown is a socket error, you can use the -L
flag to tell tmux
to use a specific socket.