Reptyr – Move A Running Process From One Terminal To Another Without Closing It
Let us say, you are running a process in a remote server via a SSH session from your local system. You didn’t know that the remote job would take long time to complete. You just want to leave the running job on the remote server itself, and close the SSH session without terminating the remote job, and then re-attach it to the SSH session later or at the next day. Of course, you can start the job in screen/tmux session, and detach from the screen session without exiting the remote job, and exit SSH session. But if you forgot to start the screen session in the first place, there is no way to reattach to the running process later. Once you closed the SSH session, the running processes will also be closed on the remote system. So, what will you do in such cases? No problem. Here is where Reptyr command comes in help.
Reptyr is a command line tool for moving running processes between ptys. Just start a process that takes long time to complete on your remote system via SSH session from your local Desktop or Laptop, and close the SSH session, go home, and re-attach the running process on the next day. Also, You can easily migrate/move a long running process from one Terminal to another Terminal instantly without having to terminate it. Sounds useful? Of course, it is!
This brief tutorial describes how to move a running process to a new terminal using Reptyr command.
Install Reptyr on your remote systems
Note: Your remote system should have tmux or screen installed in-order to move running process.
In Arch Linux and its derivatives, run:
$ sudo pacman -S reptyr
In DEB based systems, run the following command to install Reptyr:
$ sudo apt-get install reptyr
To install screen or tmux, run:
$ sudo apt-get install tmux screen
On RHEL / CentOS / Scieintific Linux / Fedora, reptyr is not available in the repositories. So, you can compile and install it as shown below.
Install the following prerequisites:
$ sudo yum install git tmux screen
$ sudo yum groupinstall 'development tools'
Git clone reptyr repository with command as root user:
$ git clone https://github.com/nelhage/reptyr.git
Go to the reptyr directory:
$ cd reptyr/
Run the following commands to compile and install it.
$ sudo make install
I compiled and installed Reptyr from source in CentOS 7 64 bit server edition, and it worked pretty good as described above.
Move A Running Process From One Terminal To Another Without Closing It Using Reptyr
Usually, we connect to the remote server from any local system via SSH as shown below.
# ssh [email protected]_Of_remote_system
After you connected to the remote system, start a long running process. For example, I am going to download Ubuntu 16.04 desktop ISO using wget command.
# wget http://cdimage.ubuntu.com/daily-live/current/xenial-desktop-amd64.iso
As you see in the above screenshot, the total download size is 1.5GB, and it will take more than 90 minutes to complete.
I don’t want to wait that much longer, and also I don’t want to quit the remote job either.
So, what I am going to do is to start a screen or tmux session in a new Terminal, use reptyr utility to grab the running process inside screen or tmux session. Finally, I will terminate both ssh sessions, and reattach to the running process whenever I want.
Let us open a new Terminal window or new tab, and start a screen or tmux session by typing screen or tmux in the Terminal:
As you see in the above screenshot, the screen session has been started and it is running.
Now, let us find the the running processes from the new Terminal by using the following command:
# ps -a
PID TTY TIME CMD 2320 pts/0 00:00:11 wget 2343 pts/1 00:00:00 screen 2358 pts/2 00:00:00 ps
Note down the PID for the wget process, and attach the running process inside screen session using command:
# reptyr 2320
Done! As you see in the above screenshot, wget process has been moved (migrated) from old Terminal to the new Terminal window (the one running with screen session).
Once you moved the running process from the original Terminal, it will be closed immediately, and start to continue where we left it off in the new Terminal.
Now, you can safely detach or close the terminal and the job will continue running on your remote server.
To detach from screen or tmux session, press CTRL+A+D.
After you detached from screen session, the following message will appear.
[detached from 2344.pts-1.server1]
To reattach the running process, SSH to your remote system:
# ssh [email protected]
Here. 192.168.1.150 is my remote server IP address.
And run the following if you use screen session:
# screen -Dr
For tmux session, run:
# tmux attach
Voila! The running process has been reattached again, and you’ll see there that the process is still running.
As you see in the above screenshot, wget job isn’t interrupted or terminated, and is still running. It will continue to run as long as your remote system is up and running.
To summing up, Reptyr is very very important and useful tool for a Linux system administrators of any level. In case you fed up with a process that took a really long time to complete, Reptyr will definitely be helpful. Just open a new Terminal window, SSH to your remote server, find the running processes ID, and safely move them inside the screen or tmux sessions, and exit from the SSH session. For further details, refer the links attached at the end of this tutorial.
That’s all for now folks. Well then, I leave you to get acquainted with this useful tool. Give it a try, and you won’t be disappointed. If you find this guide useful, share it on your social, professional networks and support OSTechNix.