In Linux, while running a process that needs a long time to run, it can be a pain to terminate it halfway because you’re out of time, or because you need to get home. If you have ever encountered a situation like this before, then you really need to get the Reptyr tool.
What is Reptyr?
The Reptyr tool is a command line utility that takes a running process from one terminal and migrates it to another. In other words, it repeats a process. The name is also reminiscent of a pseudoterminal- often abbreviated as “PTY”. A pseudoterminal, if you didn’t know, is a tool that lets two processes (a master and slave) be linked to each other. What you write on one is reflected in the other.
How Does Reptyr Work?
With Reptyr, you can start a process at work and finish it at home (if you’re using SSH). There are other tools that are similar to the Reptyr tool, like Screenify and Retty, and you may have encountered them before. However, Reptry has proven to be better than its competitors at the moment. It has fewer bugs and is far more efficient. The problem with these other tools is that the processes’ controlling terminal isn’t changed even when the process is migrated. If you use Screenify, for example, and you try using the “^c” or “^z” commands on the new terminal, they don’t work correctly. This is because they are executed on the terminal that has the control at the moment. If you resize a program window or try the “
less” command, it doesn’t work correctly either, as Linux is fooled into thinking that you’re still operating on the old terminal.
So how does Reptyr work exactly? It targets the process we’re looking to migrate using ptrace API, opens up a new terminal and uses “dup2” to overwrite the old file descriptors. Reptyr is also different from other processes in that it changes the controlling terminal. You can check the official blog to get the details but it basically involves manipulating the ioctl, TIOCSCTTY feature in Linux to change the controlling process.
In a Ubuntu-based distro, you can use apt-get to install the application:
sudo apt-get install reptyr
Moving a Process to a New Screen with Reptyr
You can also use Reptyr, along with the GNU Screen software app (or a similar terminal multiplexer application), to move your process to a new screen session. For example, if you have a running process and need to restart your server, you can transfer it to a new screen session so you can access it again after a system restart. Sounds useful, right? So how do you do that exactly?
First, you need to suspend the process that you need to migrate. That can be done in different ways – it all depends on the process you’re running (mostly “^z” works). Then, after you have suspended it, you need to resume the process in the background. The command for this, is you didn’t know, is,
Next, you will need to isolate the child process from the parent process. This is because the parent process will be terminated during the restart, and you need to keep that from happening to the child process. To do that, use the disown command:
Then you’ll have to launch the screen software application. It’s actually a pretty handy tool – it’s easy to download, if you don’t have it. You launch it with the following command:
And now you need to retrieve the process you’re trying to migrate. Please note that you need the PID of the running process for this command. Alternatively, you can use the “
pgrep” command. If you know the PID, type:
If you don’t know the PID:
reptyr $(pgrep name_of_process)
And you’re done! You can now log out from your session without killing the process.
$ ctrl+z # Suspend the current process $ disown <your process name> # Detach process from the shell $ screen # Launch screen $ reptyr $(pgrep irssi) # Get back the process
Reptyr is open source and completely free. It’s easy to setup and use, so do go ahead and give it a try. Am sure you’ll find it quite handy.