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

42

25

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.

Jürgen Paul

Posted 2013-07-24T02:59:32.130

Reputation: 715

1There is no way to do that. You have to start the command from within a screen (or other) session for it to be detachable. – Dan D. – 2013-07-24T03:16:16.260

Answers

50

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

Posted 2013-07-24T02:59:32.130

Reputation: 1 692

2Unfortunately after running sudo reptyr 1430 I still got: "...[-] Unable to open the tty in the child. Unable to attach to pid 1430: Permission denied" – Daryl Spitzer – 2014-12-12T18:55:21.443

3You may find some success with the -L option e.g. reptyr -L 1430 in your case. – cgseller – 2016-04-21T19:06:56.773

1Run this when necessary: echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope – Yanhao – 2016-05-06T11:39:30.927

it is also on Arch via yo: https://www.archlinux.org/packages/community/i686/reptyr/ (not apt-get or yum)

– Tommy – 2016-12-16T02:10:40.447

1After bg, the process should be running in the background, so jobs should say Running rather than Stopped. – G-Man Says 'Reinstate Monica' – 2017-05-24T00:49:04.670

The answer didn't work for me. This one helped: https://sayanchowdhury.dgplug.org/migrate-a-running-process-into-tmux/ – Gokul NC – 2018-02-23T16:10:52.563

This worked for me. This is some wizardry level stuff. TY! – James MV – 2019-03-13T11:32:18.970

I also got the permission denied with reptyr <pid>... but after reptyr -L <pid> I just got a command prompt... I was super confused, but then found reptyr <pid> worked – nmz787 – 2020-02-24T10:40:11.640

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

kqw

Posted 2013-07-24T02:59:32.130

Reputation: 1 781

2When I added the -L I got a message like yours saying Opened a new pty: /dev/pts/6 but my suspended top didn't appear in the tmux. what am I missing here? – Mehrad Mahmoudian – 2019-01-09T09:35:59.217

-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

Posted 2013-07-24T02:59:32.130

Reputation: 4 399

I see a possible semantic disagreement here, and this is a pretty popular search result for keywords like 'relocatable', 'position-independent', 'detach tmux', etc. so I hope you can forgive the necro-post. Did you mean "you can't" because of different memory mapping between stopping the program and resuming it? A backup program is only going to know what files to back up by the file structure it's given, and the file structure exists to make that physical memory mappable in the future. (We back up for the occasion that it doesn't!) – John P – 2017-07-06T16:43:19.377

Can you expand on this? What do you mean to start with it nohup? How does disowning a process work? – Darth Android – 2013-07-24T15:40:46.737

disown detaches the background process (that you put into background with control-z) from the terminal so the process will continue after the terminal is gone. you can just type disown after control-z, if the command is found. however this is in theory and may not work reliably. next time it is safer by adding nohup in the front of your program so that you can exit the terminal safely. – johnshen64 – 2013-07-24T15:50:33.673

I've found 'disown' to be pretty unreliable, as mentioned. nohup isn't really hugely better. reptyr might be a solution however. https://github.com/nelhage/reptyr

– anastrophe – 2013-09-20T23:58:24.797

This does not answer the question. The question is not only "how do I close the terminal without killing the backup", it is "how do I resume it later". To which the correct answer is "you can't". – Gabe – 2013-11-11T23:51:40.323