Link to: http://www.ostechnix.com/reptyr-move-running-process-new-terminal/
Let us say, you are running a long process in a remote server via a SSH session from your local system. You didn’t expect the remote job would take too long to complete. You just want to leave the running job on the remote server, and close the SSH session without terminating the remote job which is running in your remote server, and then re-attach to the SSH session later or on 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. Once you closed the SSH session, the running processes will also be closed. So, what will you do in such cases? No worries, where there is will, there is a way. Here is where Reptyr command comes in help.
Reptyr is a simple 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? Yes, it should be.
Note: Your remote system should have tmux or screen installed in-order to move running process.
This brief tutorial describes how to move a running process to a new terminal using Reptyr command.
Install Reptyr on your remote systems
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:
yum install git tmux screen
yum groupinstall 'development tools'
Git clone reptyr repository with command as root user:
git clone https://github.com/nelhage/reptyr.git
Cloning into 'reptyr'... remote: Counting objects: 948, done. remote: Total 948 (delta 0), reused 0 (delta 0), pack-reused 948 Receiving objects: 100% (948/948), 228.58 KiB | 150.00 KiB/s, done. Resolving deltas: 100% (510/510), done.
Go to the reptyr directory:
Run the following commands to compile and install it.
I compiled and installed Reptyr from source in CentOS 7 64 bit, and it worked like a charm.
Typically, we connect to the remote server from any local system via SSH as shown below.
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.
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 started and 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:
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:
Here. 192.168.1.150 is my remote server IP address.
And run the following if you use screen session:
For tmux session, run:
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 tool for a Linux system administrator. In case you fed up with long running process, it will definitely help. 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 given 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.