It’s rare, but sometimes it still happens that I forget to open a tmux or screen session when working with something that is supposed to be quickly done. However, it also happens that “quickly done” turns into “tedious and ugly” and now the process lives longer than it was supposed to and I become afraid of ssh disconnects or something.

So an obvious solution is killing the process and running it in a newly created tmux session — but what if the process ran for a while and I don’t want to kill it because I either lose progress or end up in a mess? Instead of killing and re-running a process, it would be much smoother to just move it into a tmux session. This involves changing the parent of a process, which is not exactly trivial, but thankfully @nelhage made a tool for that: reptyr. If you’re interested in how reptyr actually achieves its goal, check out his blog posts12!

As for usage, it is very easy:

  1. Suspend the respective process with Ctrl-Z
  2. Send the job to background using bg
  3. Take away the ownership from the shell using disown
  4. Start or enter your tmux/screen session
  5. Run reptyr PID to attach the process to the current shell

It also has some additional useful features, such as TTY-stealing, which is documented in the man page.

Before compiling reptyr, make sure to check whether it is in you distributions repository. At the time of writing, this was at least the case for Fedora and Debian.