reptyr: Reparent a Running Process to a New Terminal

2026-04-23

It's 2 AM. You SSH'd into a server, kicked off a massive database migration, and now you realize you forgot to run it inside tmux. Your laptop battery is dying. If your SSH session drops, that process dies with it. You feel the cold hand of regret on your shoulder.

reptyr solves exactly this nightmare. It takes a running process and reparents it — yanking it out of its current terminal and attaching it to a new one. Written by Nelson Elhage, it works by using ptrace() to seize the target process and rewire its file descriptors so stdin/stdout/stderr point at your new terminal.

Install it:

apt install reptyr      # Debian/Ubuntu
pacman -S reptyr        # Arch
brew install reptyr     # macOS (limited support)

The classic save-my-session workflow:

# Terminal 1: You started a long job without tmux. Oops.
$ ./migrate_everything.sh
Processing table 41 of �...

# Step 1: Suspend the process
Ctrl+Z
[1]+  Stopped  ./migrate_everything.sh

# Step 2: Background it and disown it from this shell
$ bg
$ disown

# Step 3: Note the PID
$ ps aux | grep migrate
shaun  48291 ... ./migrate_everything.sh

# Step 4: Open tmux (or screen), then grab the process
$ tmux
$ reptyr 48291

The process is now running inside tmux. You can detach, close your SSH connection, go to bed, and reconnect tomorrow. The migration keeps running.

One gotcha: On modern Linux, ptrace scope is restricted by default. If you get a permissions error:

# Temporary (until reboot):
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

# Permanent:
echo 'kernel.yama.ptrace_scope = 0' | sudo tee /etc/sysctl.d/10-ptrace.conf
sudo sysctl -p /etc/sysctl.d/10-ptrace.conf

There's also a -T flag that uses a different, more aggressive technique. Instead of ptrace-based fd manipulation, it creates a new pseudo-terminal and uses TIOCSTI ioctl calls to "type" into the target process's controlling terminal. This is useful when the standard method fails:

# If regular reptyr fails, try:
$ reptyr -T 48291

What about the alternatives?

reptyr is the only tool that gives you both: the process stays alive and you get full interactive control in a new terminal.

A few more tricks worth knowing:

# Attach to a process running in another tmux pane
$ reptyr $(pgrep -f "python train_model.py")

# Works with interactive programs too — editors, REPLs, etc.
$ reptyr $(pgrep -f "python3 -i")

The tool is tiny (a few hundred lines of C), has no dependencies, and compiles on any Linux box. It's one of those utilities you install on every server before you need it, because by the time you need it, you really need it.

Key Takeaway: reptyr PID rescues a running process from a doomed terminal by reparenting it into your current one — the only tool that lets you retroactively move a process into tmux after you forgot to.

All newsletters