Reptyr – Moving Processes to Another Screen Shell [Linux]

Spread the love

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.

Installation

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,

bg

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:

disown name_of_process

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:

screen

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:

reptyr PID

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.

Recap

$ 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</your>

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.

Image credit: Techiezone, Mtellin@Flickr

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


Richa

I’m a techie with over a decade of programming experience, spread across a wide range of interesting, path breaking technologies. Now I’m sharing my passion for technology, and making tech easier, with everyone! Hope you enjoy reading about, and playing with technology, as much as I do!

Comments (1)