reptyr: move programs between terminals

terminalreptyr solves a very common problem faced by terminal users on Linux – how to move a running process from one terminal to another? reptyr does a more thorough job of transferring programs than many other tools because it changes the program’s controlling terminal. This means that actions such as window resizes and interrupts are sent to the process from the new terminal. It is very useful for moving a long-running process into a GNU screen session and for users like me out there, it is absolutely a must. With reptyr you can end a ssh session without terminating the process running in it on the remote machine by just attaching the ssh session to a new terminal.

Here’s another use case from my personal experience: often I fire different programs in different terminals but when the task in the terminal I am currently on is over, I like to pull back the another task to this terminal without having to switch terminals. reptyr does this for me.

It’s worthy to mention screenify here, a small script to make processes talk to the current terminal. But it suffers from problems like some processes still take inputs from the older terminal, resizing the new terminal doesn’t affect a ncurses based program or <Ctrl-c> won’t work in the newer terminal. reptyr takes care of all these issues too!

Install reptyr on Ubuntu:

$ sudo apt-get install reptyr

To attach a process to a terminal, run the following command from the terminal:

$ reptyr PID

How to get the PID of a process? Simple! Run the following:

$ ps -ax | grep process_name
$ ps -ax | grep ssh

reptyr can also create a new pseudo-terminal pair with nothing attached to the slave end, and print its name out. Run:

$ reptyr -l

In case you keep getting the following error with reptyr (I faced this on Ubuntu 14.04):

$ reptyr 1851
Unable to attach to pid 1851: Operation not permitted
The kernel denied permission while attaching. If your uid matches
the target's, check the value of /proc/sys/kernel/yama/ptrace_scope.
For more information, see /etc/sysctl.d/10-ptrace.conf

It means that your kernel is running in a lesser permissive mode when it comes to attaching processes. Only attaching direct child processes are allowed to harden the kernel. To get reptyr working (at your own risk), edit /etc/sysctl.d/10-ptrace.conf, and set:

kernel.yama.ptrace_scope = 0

Then reload the sysctl rule:

$ sudo sysctl -p /etc/sysctl.d/10-ptrace.conf

Webpage: reptyr

One thought on “reptyr: move programs between terminals”

Leave a Reply

Your email address will not be published. Required fields are marked *