Transfer current command to a detachable session (tmux/screen)

Solution 1:

This works, most of the time:

Prerequisites: have reptyr and tmux/screen installed; you'll be able to find them with apt-get or yum, depending on your platform.

  1. Use Ctrl+Z to suspend the process.

  2. Resume the process in the background with bg

  3. Find the process ID of the background process with jobs -l

    You'll see something similar to this:

    [1]+ 11475 Stopped (signal) yourprocessname
    
  4. Disown the job from the current parent (shell) with disown yourprocessname

  5. Start tmux (preferred), or screen.

  6. Reattach the process to the tmux/screen session with reptyr:

    reptyr 11475
    
  7. Now you can detach the multiplexer (default Ctrl+B, D for tmux, or Ctrl+A, D for screen), and disconnect SSH while your process continues in tmux/screen.

  8. Later when you connect with SSH again, you can then attach to your multiplexer (e.g. tmux attach).

Solution 2:

reptyr is good but I got a permission error

$ reptyr 30622

[-] Unable to open the tty in the child.
Unable to attach to pid 30622: Permission denied

Then found
-L Like '-l', but also redirect the child's stdio to the slave.

which worked like a charm

$ reptyr -L 30622
Opened a new pty: /dev/pts/4