82

I'm currently running a backup and it now needs to be transferred to detachable one like on tmux or screen. Is there a way to do this when the command is currently running?

I can send the command the background by pressing Ctrl+Z and put it back up by issuing a fg command. but I do not know if that session can go back when I exit the terminal.

the
  • 2,929
Jürgen Paul
  • 1,145

3 Answers3

95

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).

Ville
  • 3,492
  • 3
  • 24
  • 20
5

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
the
  • 2,929
-1

you can use disown to detach the job from its terminal, if that command is available.

it is safer to run it with nohup to start with though.

johnshen64
  • 4,701