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
You should look for modern alternatives like .tmux
is superior to tmux for many reasons, here are just some examples:screen
To get the same functionality as explained in the answer recommending , you would need to do the following:screen
tmux by typing tmux into the shelltmux sessiontmux session by typing Ctrl+b and then dYou can now safely log off from the remote machine, your process will keep running inside . When you come back again and want to check the status of your process you can use tmux to attach to your tmux attach session.tmux
If you want to have multiple sessions running side-by-side, you should name each session using Ctrl+b and . You can get a list of the currently running sessions using $ or simply tmux list-sessions, now attach to a running session with command tmux ls.tmux attach-session -t <session-name>
can do much more advanced things than handle a single window in a single session. For more information have a look in tmux or the tmux GitHub page. In particular, here's an FAQ about the main differences between man tmux and screen.tmux
Consider :reptyr
NAME
reptyr - Reparent a running program to a new terminal
SYNOPSIS
reptyr PID
Find vim's process id with or pidof vim.ps aux | grep vim
It is also possible to see the process id if you try to open a file which is still opened by vim. Using the shown pid you should be able to reattach to your previous session.
If you get an error:
Unable to attach to pid 12345: Operation not permitted
Ensure that the is set to ptrace scope (instead of 0):1
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
After running , you may have to press Enter or another key to refresh the terminal (console) window.reptyr
The answer was noted in this post.
Sending an signal to the SIGUSR1 process recreases the socket file, allowing you to reattach using tmux afterwards.tmux attach
Since the PID of the running command was tmux, I used:5612
$ sudo kill -SIGUSR1 5612
$ tmux attach
The name of the session is stored in the tmux variable , to access it in a terminal, you can do#S
tmux display-message -p "#S"
If you want to use it in , it's simply .tmux.conf. Note that the #S option will print the message on stdout, otherwise the message is displayed in the tmux status line.-p
If the above command is called inside a session, it returns the name of the session. If it is called outside any session, it still returns the name of the last still running session. I couldn't find a tmux command to check, if one is inside a session or not, so I had to come up with this work around:
tmux list-sessions | sed -n '/(attached)/s/:.*//p'
shows all sessions, if one is attached, it shows tmux list-sessions at the end. With sed we suppress all output (option (attached)) except where we find the keyword -n, at this line we cut away everyhing after a (attached), which leaves us with the name of the session. This works for me inside and outside a session, as opposed to :.tmux display-message -p "#S"
Of course this works only if there is no and no : in the name of the session.(attached)
As commented by Chris Johnsen, a way to check if one is inside a tmux session is to see if its environment variable is set:
[[ -n "${TMUX+set}" ]] && tmux display-message -p "#S"