If you are a Linux or cloud administrator or anyone whose life revolves around a Linux terminal, then you need to LEARN about Reptyr, RIGHT NOW!
You might be thinking, what the heck is this tool and why should you use it? What void space does it fill? Read the complete article and you will find your answer with a great sense of relaxation that you have never felt before.
First, let me introduce you to the Reptyr tool in simple terms.
Table of Contents
What is the Reptyr Tool in Linux?
Reptyr is a free and open-source command-line tool to move your currently running process from one terminal session to another without terminating it.
It takes the use of the ptrace system API to attach the running process in the current shell. So, a long-running process can be easily moved from one terminal session to another.
Real-Life Example of Reptyr Tool Usage
Let me give you a scenario. You have been working on a remote machine through an SSH connection. And suddenly you started a long-running process like taking a backup or downloading a big file using the wget
command.
When you check the clock, you will see that you are out of time and this process will take longer than expected. So, in a normal case, what should you do to keep the process running without having an SSH connection?
Your answer might be to start the running process in the Tmux or Screen session, detach the session without exiting the remote job, and exit the SSH session.
But it’s too late to realize that you have started your process outside of the Tmux or Screen session, and if you close the connection in the middle of the working process, then you will lose your working process.
So, to tackle these issues, you can take the help of Reptyr and move your currently running process into a Tmux or Screen session, and then detach the session and exit your SSH connection.
So, next time you log in to your remote machine through an SSH connection, you will find your process is still running without being closed.
Performing Reptyr, Practically in a Linux Environment
For demonstration purposes, we will perform the below steps to make it understandable for you.
- SSH into a remote machine.
- Install Reptyr and Tmux or Screen multiplexer.
- Starting the demo process like VIM editor (and writing some random content in it).
- Then we will suspend the VIM process in the background.
- Detach it from the current terminal session.
- Open the Tmux or screen multiplexer.
- Lastly, we will attach the background VIM process to the current Tmux or screen session.
After knowing the flow of this demo practice, you can easily understand what you are about to learn.
How to Move a Running Process From One Terminal to Another Terminal Session Without Closing It Using Reptyr
First, find your remote machine’s username and IP address. In my case, it’s 192.168.1.10
and, for your information, my local machine is installed with Ubuntu 22.04 and the remote machine with Fedora 36.
Step 01: Creating an SSH Connection to the Remote Machine
First, run the following command to create an SSH connection with your remote machine (Fedora 36).
$ ssh [email protected]
Below is the output of the above command with some explanation.
Step 02: Installing Reptyr on a Remote Machine
It is available for major Linux distributions and can be easily installed from their default package manager. Follow any of the below methods depending upon your distribution.
Users of Ubuntu, Debian, Linux Mint, and Pop!_OS can easily install it using the APT package manager.
$ sudo apt install reptyr
RHEL, Fedora, CentOS, AlmaLinux, and Rocky Linux users can use the DNF package manager for installation.
First, install the EPEL repository on your system by passing the following command.
$ sudo dnf install epel-release
After the EPEL repository is installed and enabled, run the following command to install Reptyr.
$ sudo dnf install reptyr
Users of Arch, Manjaro, and Endeavour OS can use the Pacman package manager.
$ sudo pacman -Sy reptyr
For Tmux or Screen installation, you can run the following commands.
For Tmux installation:
$ sudo apt install tmux
$ sudo dnf install tmux
$ sudo pacman -Sy tmux
For Screen installation:
$ sudo apt install screen
$ sudo dnf install screen
$ sudo pacman -Sy screen
Step 03: Start the Demo Process (Example: VIM)
Let’s launch the VIM editor, or you can also execute the top
command, whatever you prefer. In my case, I will launch the VIM and write some content as shown.
$ vim
After the application launches, I’ve written the following content for demo purposes.
Step 04: Suspending the Active Process (Example: VIM)
As our application is running, you have to put the process in the background by using the Ctrl+z shortcut key.
To check the process in the background, run the following command.
$ jobs -l
Below is the output of the above command.
Now make sure to take note of your background process ID (for example: 5277
), which we will later use to attach in another session.
Step 05: Detaching it from the Current Shell Session
For now, the VIM process is attached to the current shell session. If it is closed, then you will lose your background process. To avoid that, you need to pass the following command to disown the running process from the current parent session.
$ disown vim
After executing the above command, you will get the following output.
If you check your current process by using the jobs
command, it will not show. However, you can use the ps
command to check all your background processes.
$ ps -a
Below is the output of the above command.
If you look above, you can find the disowned VIM process with the PID 5277
.
Step 06: Starting a Tmux or Screen Session
Let’s start a Tmux session in the same terminal window by using the following command.
$ tmux
Below is the output of the above command.
Step 07: Attach the Background Process to the Current Tmux Session
Remember in step 04, we took the note of background process ID of the VIM by using the ps
command. If you didn’t note it, then don’t worry, just re-execute the ps -a
command and take note of the VIM PID.
Now attach the VIM background process in the current Tmux session by using the reptyr
command.
$ reptyr 5277
Below is the output of the above command.
Congratulation!!! You have successfully moved the working process from one terminal session to another.
Now you can safely detach from the Tmux session by using the Ctrl+B and then D keyboard keys. It will only detach the Tmux session without closing any running processes within it.
Issue the following command to verify the list of active Tmux panes.
$ tmux list-panes -F '#{pane_active} #{pane_pid}'
1 5840
Whereas:
pane_active will show the number of opened panes.
pane_pid will show the PID of the first process in the pane.
Now you can close your SSH connection and come back whenever you want, and just execute the following command to reattach the detached Tmux session.
$ tmux attach
Last Word
I hope you understood the working of the reptyr
command. If you have any questions or if I missed something that you know and everyone should know. Then do us a favour and let us know in the comment section.
Innovative tech mind with 12 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.