LinuxCommandLibrary

reptyr

Reattach running programs to a new terminal

TLDR

Move a running process to your current terminal

$ reptyr [pid]
copy

Attach to a process using its name
$ reptyr $(pidof [htop])
copy

SYNOPSIS

reptyr [OPTIONS] PID

PARAMETERS

-s
    Suppress reptyr's output, only printing errors.

-L FILENAME
    Log reptyr's debug output to the specified FILENAME.

-P
    Do not attempt to change the process's controlling terminal. This can be useful if the process doesn't need a new TTY or if changing it causes issues.

-T
    Do not try to reattach the process's controlling terminal. This is typically done automatically to ensure full terminal control.

-d
    Do not detach the process from its current TTY. Use with extreme caution as this can lead to unpredictable behavior or conflicts.

-o
    Attempt to close all other open file descriptors in the target process besides stdin/stdout/stderr. This can be aggressive and potentially risky.

-u
    Do not close the process's old stdin/stdout/stderr file descriptors. Use with caution as this may lead to resource leaks or unexpected interactions.

-A
    Acquire a TTY for the process by acquiring the new TTY that reptyr itself is running in, rather than trying to get the target process's original TTY. Useful in advanced scenarios.

-c
    Continue the target process even if it is currently stopped.

-e VAR=VAL
    Set an environment variable VAR to VAL in the target process's environment. Can be specified multiple times.

-v
    Print reptyr's version number and exit.

-V
    Print the ptrace version number and exit.

-h
    Display a help message and exit.

DESCRIPTION

reptyr is a powerful utility that allows you to attach a running program to a new terminal. This is particularly useful in scenarios where a long-running process was started in a terminal session that is about to be closed, or if you simply want to move the process to a different terminal without interrupting its execution. It achieves this by using the ptrace system call to seize control of the target process, redirecting its standard input, output, and error streams to the new terminal. It effectively "reparents" the process, making it behave as if it was launched from the new terminal.

While nohup and shell job control (like disown) can prevent processes from terminating when a shell exits, they don't allow for re-establishing interactive control. reptyr fills this gap, providing a way to take over a process's console I/O even if it wasn't initially started in a persistent session like screen or tmux. This capability makes reptyr an invaluable tool for system administrators and developers who need flexibility in managing background processes.

CAVEATS

  • Root Privileges: reptyr typically requires root privileges or the CAP_SYS_PTRACE capability to attach to processes owned by other users or if the system's ptrace_scope is restricted.
  • Security Context: Reparenting a process changes its controlling terminal, which might have security implications or affect programs that rely on specific terminal properties.
  • Unstable State: While generally robust, reptyr can sometimes leave the target process in an unstable state, especially if the process was designed to interact directly with its original terminal in complex or unusual ways.
  • File Descriptors: reptyr focuses on standard I/O. Other open file descriptors in the process (e.g., pipes, sockets, device files) that are tied to the old terminal or session remain untouched, which might cause issues. The -o option attempts to mitigate this but can be risky.
  • PID Stability: The attachment applies only to the specified PID. If the process forks or executes new binaries, child processes might not be affected as expected or might still be tied to the original terminal.

<I>HOW IT WORKS (ADVANCED)</I>

reptyr operates by utilizing the Linux ptrace system call. It attaches to the target process, then manipulates its file descriptors for standard input (0), output (1), and error (2), redirecting them to the new controlling terminal where reptyr itself is running. It also attempts to change the process's controlling terminal group. This manipulation often involves injecting code into the target process's memory space to perform necessary system calls (like fcntl) to close old descriptors and open new ones, effectively giving the process a new 'home' terminal.

<I>PERMISSIONS AND PTRACE SCOPE</I>

For reptyr to successfully attach to a process, the user running reptyr must have sufficient permissions to debug the target process. This typically means:

  • Running reptyr as the root user.
  • The invoking user owns the target process.
  • The system's ptrace_scope setting (found at /proc/sys/kernel/yama/ptrace_scope) is configured to allow `ptrace` operations between processes. A value of 0 usually allows tracing any process by any user, while a value of 1 (the default on many modern systems) restricts tracing to children of the tracer, or processes that are owned by the same user.

HISTORY

reptyr was originally developed by Nelson Elhage and publicly released around 2011. Its primary motivation was to provide a flexible solution for a common user dilemma: forgetting to launch a long-running command within a persistent terminal multiplexer like screen or tmux. Prior to reptyr, users would often have to kill and restart such processes if their original terminal session was closed. By leveraging the low-level ptrace system call, reptyr filled a crucial gap, offering an on-demand mechanism to migrate a running process's I/O to a new terminal without interrupting its execution. Its development focused on being lightweight and effective, making it a popular choice for system administrators and developers.

SEE ALSO

nohup(1), screen(1), tmux(1), setsid(1), disown(1), ptrace(2)

Copied to clipboard