The Lost Feed

🔬Weird Science

Move Running Programs Into Tmux Sessions Easily

Learn the simple trick to move active programs into a tmux session without stopping them. Keep your work going!

13 views·7 min read·Jul 8, 2026
How to move a running process into a tmux session (2020)

Have you ever started a long-running program on your computer, only to realize you need to close the terminal or switch to another task? It's a common frustration. You might think you have to stop the program, lose your progress, and start over. But what if there was a way to save your work and keep it running in the background, even if you close your current window?

This is where a powerful tool called tmux comes in handy. It's a way to manage multiple terminal windows and keep programs running even after you log out. But the real magic happens when you can take a program that's *already

  • running and move it into a tmux session without missing a beat.

What is

Tmux and Why Use It?

Tmux stands for Terminal Multiplexer. Think of it like a super-powered tab system for your command line. Instead of just one terminal window, tmux lets you create multiple windows and panes within a single terminal session. This is incredibly useful for organizing your work.

But the benefits go beyond just organization. One of the best features of tmux is its ability to *detach

  • from a session. This means you can start a program, leave your computer, and come back later to find that program still running exactly where you left it. You can then *reattach

  • to the session and continue working as if nothing happened.

This is a lifesaver for tasks that take a long time, like compiling code, running large data analysis, or downloading big files. No more worrying about your internet connection dropping or accidentally closing the wrong window.

The Challenge:

Moving a Live Process

So, tmux is great for starting new things inside it. But what about programs that are already running *outside

  • of tmux? Maybe you forgot to start tmux, or a script was kicked off automatically. You want to bring that live process under tmux's protection, but how?

Stopping a running program can be difficult. Some programs don't save their state easily. Others might take hours to restart. You don't want to interrupt their work. The goal is to *seamlessly transfer

  • the running process into a new tmux environment.

This is where a clever trick comes into play. It involves using a few standard Linux commands to trick the running program into thinking it's still in its original environment, while actually placing it within a new tmux session.

The Key Command: reptyr

To achieve this, we need a special tool. The most effective command for this job is called reptyr. This command is designed to *re-parent

  • a running process to a new terminal. Think of it like telling a child, "You're now playing in this new sandbox, but you're still the same kid."

Before you can use reptyr, you might need to install it. The installation process varies depending on your operating system. On many Linux distributions, you can install it using your package manager.

For example, on Debian or Ubuntu based systems, you would typically run:

sudo apt-get update
sudo apt-get install reptyr

On Fedora or CentOS, you might use:

sudo dnf install reptyr

Or on Arch Linux:

sudo pacman -S reptyr

Always check your system's documentation if these commands don't work. Sometimes, reptyr might be part of a larger package or require compiling from source.

Preparing Your System for reptyr

For reptyr to work its magic, your system needs to allow this kind of process manipulation. This is a security feature to prevent malicious programs from messing with others. You usually need to make a small change to your system's kernel parameters.

This involves enabling something called ptrace for processes that are not owned by the current user. You can do this by writing a value to a specific file in the /proc filesystem.

First, you need to find the Process ID (PID) of the program you want to move. You can find this using commands like ps aux or pgrep. Let's say the PID is 12345.

Then, you would typically run a command like this (you might need sudo):

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

This command temporarily sets the ptrace_scope to 0, which allows processes to be traced by any other process. *This change is usually temporary

  • and might reset after a reboot. For a permanent change, you would need to edit system configuration files, but for a quick transfer, this temporary setting is often sufficient.

Be aware that changing security settings can have implications. It's generally recommended to revert this setting after you're done if you're not sure about the long-term effects.

The Step-by-Step Process

Now that you have reptyr installed and your system is configured, let's walk through the actual steps to move a running process.

  1. *Start a tmux session:
  • If you don't already have one, open your terminal and type tmux to start a new session. You can name it if you like, for example, tmux new -s my_session.
  1. *Find the PID of the target process:
  • Use ps aux | grep your_program_name or pgrep your_program_name to find the Process ID (PID) of the program you want to move. Let's assume the PID is 12345.
  1. *Detach from tmux (if attached):
  • If you are currently inside the tmux session you want to move the process to, detach from it. Press Ctrl+b followed by d. This returns you to your original terminal prompt, but the tmux session keeps running in the background.
  1. *Run reptyr:
  • Now, from your *original

  • terminal (the one *outside

  • of tmux), run the reptyr command with the PID you found:

reptyr 12345

If you get a permission error, double-check the ptrace_scope setting mentioned earlier. You might need to run echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope first.

  1. *Reattach to your tmux session:
  • After reptyr runs successfully, the process should now be attached to the tmux session. To see it, reattach to your tmux session:
tmux attach -t my_session

(Replace my_session with the name you gave your session, or just tmux attach if you didn't name it).

And there you have it! The program that was running in your original terminal should now be visible and controllable within your tmux session. You can interact with it just as if you had started it there originally.

What If reptyr Isn't Working?

Sometimes, reptyr might not work as expected. There are a few common reasons for this.

  • *Permissions:
  • As mentioned, the ptrace_scope setting is crucial. If it's set too restrictively, reptyr will fail. Ensure it's set to 0 temporarily if needed.

  • *Process State:

  • reptyr works best on processes that are actively running and waiting for input or doing standard work. Very specialized processes or those in unusual states might not transfer well.

  • *System Differences:

  • Some operating systems or specific kernel versions might handle ptrace differently, making reptyr less effective or requiring different setup steps.

  • *Security Software:

  • Aggressive security software or firewalls could potentially interfere with reptyr's ability to manipulate processes.

If reptyr fails, you might need to look for alternative methods. Some advanced users might explore tools like gdb (the GNU Debugger) to achieve similar results, but this is significantly more complex. For most common use cases, reptyr is the go-to solution.

The

Power of Continuity

Being able to move a running process into a tmux session is a powerful technique. It fundamentally changes how you can manage your command-line work. You gain the freedom to close windows, switch machines, or even reboot your system (if the process itself can handle being stopped and restarted gracefully, which reptyr helps with by keeping it alive) without losing your active tasks.

This capability is especially valuable for developers, system administrators, and anyone who spends a lot of time working in the terminal. It removes a layer of anxiety about losing work and allows for more flexible and robust workflows.

So, the next time you find yourself needing to preserve a long-running command, remember the trick. Install reptyr, adjust your ptrace_scope if needed, and bring that process into the safe, persistent world of tmux. Your future self will thank you for it.

How does this make you feel?

Comments

0/2000

Loading comments...