You can not re-attach a process id. You need to reattach the corresponding session.tmux
So do . Pick whatever session you want to re-attach. Then do tmux ls to re-attach it to a new tmux instance and release it from the old one.tmux attach -d -t <session id>
You can list tmux sessions when reconnecting to the remote server:
tmux ls
0: 4 windows (created Tue Oct 15 07:55:07 2019) [255x62]
1: 2 windows (created Tue Oct 15 07:55:07 2019) [255x62]
You can then reattach to a target session:
tmux attach-session -t 1
This would attach to the second tmux instance listed above.
If you just have one tmux session running, then you can use the shorthand:
tmux a
to reattach the first session.
This works, most of the time:
Prerequisites: have and reptyr/tmux installed; you'll be able to find them with screen or apt-get, depending on your platform.yum
Use Ctrl+Z to suspend the process.
Resume the process in the background with bg
Find the process ID of the background process with jobs -l
You'll see something similar to this:
[1]+ 11475 Stopped (signal) yourprocessname
Disown the job from the current parent (shell) with disown yourprocessname
Start (preferred), or tmux.screen
Reattach the process to the /tmux session with reptyr:screen
reptyr 11475
Now you can detach the multiplexer (default Ctrl+B, D for , or Ctrl+A, D for tmux), and disconnect SSH while your process continues in screen/tmux.screen
Later when you connect with SSH again, you can then attach to your multiplexer (e.g. ).tmux attach
There is no way, but to prevent this I like using . I start tmux, start the operation and go on my way. If I return and find the connection has been broken, all I have to do is reconnect and type tmux.tmux attach
Here's an example.
$ tmux
$ make <something big>
......
Connection fails for some reason
Reconect
$ tmux ls
0: 1 windows (created Tue Aug 23 12:39:52 2011) [103x30]
$ tmux attach -t 0
Back in the tmux sesion
Initially, you have a terminal open, with bash running in it. I'll call this the original bash.
You run from the original bash. Tmux runs two processes: the tmux server and the tmux client. The reason it does that is that you can detach from a tmux session and reattach to it later — this is a core feature of tmux. The tmux client connects to the terminal where the session is attached. The server runs the processes running in the tmux windows. When you detach from a session (tmux), the client exits, but the server and the processes running in the session keep running. In addition to the two tmux processes, assuming you haven't started a tmux session yet, C-b d creates a new session containing one window where it runs your shell: that's the second bash.tmux
At this point, the relevant part of the process tree looks like this:
…
├─tmux: server───bash───pstree
└─xterm───bash───tmux: client
This is from the command on Linux. To get a similar display on macOS, see https://apple.stackexchange.com/questions/11770/linux-ps-f-tree-view-equivalent-on-osx. pstree is the terminal where I ran my original bash, and xterm is the tmux client started by running tmux: client in the original bash. tmux is the tmux server that the tmux client started, and its child tmux: server is the second bash, where I ran bash from.pstree
On Linux, here's the output of inside tmux:ps
PID TTY TIME CMD
108 pts/56 00:00:00 bash
153 pts/56 00:00:00 ps
By default, Linux's only shows the processes that are running on the current terminal. That's why I see. On FreeBSD, and I expect also on macOS (I ran this on a machine where my shell is zsh, that's why it shows zsh rather than bash):ps
19690 0 Is 0:00.36 /usr/local/bin/zsh
20046 0 I+ 0:00.01 tmux: client (/tmp//tmux-1001/default) (tmux)
20049 1 Rs 0:00.06 /usr/local/bin/zsh
20138 1 R+ 0:00.01 ps
The reason the BSD shows more processes is that by default, it shows processes that are attached to any terminal. (The technical term is processes that “have a controlling terminal”.) It doesn't show processes that are not attached to a terminal at all, such as the tmux server.ps
To see all the processes involved and to get more information about these processes, let's run with a few options. The options to see the relevant data are slightly different on different unix variants. I'll show Linux and FreeBSD; macOS is probably close to FreeBSD but may be a little different. On Linux, here's the output of ps, filtered to the relevant processes:ps x -o pid,ppid,tty,comm f
PID PPID TT COMMAND
107 1 ? tmux: server
108 107 pts/1 \_ bash
154 108 pts/1 \_ ps
3 1 ? xterm
6 3 pts/0 \_ bash
105 6 pts/0 \_ tmux: client
And on FreeBSD, with (on macOS you'll need to remove ps -U $(id -u) -A -o pid,ppid,tty,command -d and you won't get the tree presentation) (again filtered; on FreeBSD I'm logged in remotely so the original zsh is in a terminal provided by -d):sshd
PID PPID TTY COMMAND
19689 19687 - sshd: gilles@pts/0 (sshd)
19690 19689 pts/0 - /usr/local/bin/zsh
20046 19690 pts/0 `-- tmux: client (/tmp//tmux-1001/default) (tmux)
20048 1 - tmux: server (/tmp//tmux-1001/default) (tmux)
20049 20048 pts/1 - /usr/local/bin/zsh
20149 20049 pts/1 `-- ps -U 1001 -A -o pid,ppid,tty,command -d
In both case you can see six processes:
pts/0.pts/0.pts/0, started from the original shell.PPID) is process 1. (This happens because when the tmux client starts the server, it double-forks: it creates a child process which itself creates child process, then exits immediately. When the intermediate process exits, the grandchild becomes orphaned and is therefore adopted by init, which is process 1.)pts/1.ps running inside the shell inside tmux.You kill the tmux process. But which one? We saw above that there are two. We also saw above that on macOS, with no options shows all processes that are running in a terminal. So you saw, and killed, the tmux client. This is equivalent to detaching from the session. The tmux server is still running. That's one of the reasons to use tmux: if the client is killed, for example because the terminal where the client is running goes away, the session keeps running.ps
You can see the existing tmux session with . And you can reattach to it with tmux list-sessions. If you have several sessions, you can choose which one to attach to by passing the session number after tmux attach, e.g. tmux attach to attach to the session that tmux attach 0 describes as tmux list-sessions.0: 1 windows (created …) …
If you want to kill the session without attaching to it, you can use . There's even a command tmux kill-session which kills all the sessions.tmux kill-server
When you press Ctrl+D or enter in the shell inside tmux, the shell exits. When the main process of a tmux window exits, tmux closes the window. When tmux closes the last window, the session exits.exit
Try
byobu -S wiki ls
to see if the tmux session is still there. It probably is given your ps output, and will be listed as session 1. Then try
byobu -S wiki attach-session -t 1
Maybe specify the full path to where you can see the "wiki" unix socket that was created when you did , which will be in whatever directory you executed that command in originally. Other behavior may occur if byobu or tmux was customized, but basically after you specify byobu -S wiki everything following is going to get passed to tmux. tmux just needs byobu to tell it where to look for its sockets, otherwise it will look in TMPDIR or something by default.byobu -S <path-to-put-socket|path-to-existing-socket-including-socket-name>
Hope this helps. This is my first ever stackoverflow question that (1) wasn't already answered many different ways, and (2) I knew a possible answer or at least a pointer in the right direction. :)
The answer is much simpler. Just put this in your file:~/.tmux.conf
# if run as "tmux attach", create a session if one does not already exist
new-session -n $HOST
If you run and there is a session, then it will attach to that session (whether it's already attached or not). If there is not a session already then it will create one for you.tmux attach