LinuxCommandLibrary

dtach

Detach and reattach programs from terminal

SYNOPSIS

dtach [-Eeo] -c socket command [arg...]
dtach [-Eeo] -a socket
dtach -k socket

PARAMETERS

-c socket command [arg...]
    Create a new dtach session using the specified Unix domain socket path, and run the command with its arguments within it. If the socket already exists, this command will attempt to attach to it instead.

-a socket
    Attach to an existing dtach session identified by the specified Unix domain socket path.

-k socket
    Kill the dtach server process associated with the specified Unix domain socket path, effectively terminating the session and the running command.

-E
    Do not erase the screen on attach.

-e
    Erase the screen on attach (this is the default behavior).

-o
    Use the old-style (pre-0.9) protocol.

DESCRIPTION

dtach is a minimalistic program designed to detach a running process from its controlling terminal and allow reattachment from another. It serves as a lightweight alternative to more feature-rich terminal multiplexers like screen or tmux.

The core functionality of dtach revolves around a Unix domain socket: a process is started (using -c) with its standard I/O redirected through this socket. When the original terminal disconnects or closes, the process continues running, managed by dtach via the socket. Later, from a different terminal, you can reattach (using -a) to the same socket, regaining control and interaction with the persistent process. This makes dtach ideal for managing long-running commands, shell sessions, or remote processes that need to survive network disconnections or terminal closures, all with minimal resource overhead.

CAVEATS

dtach offers no window management, scrolling, copy-paste buffers, or other advanced features found in screen or tmux. It's designed for simple detachment and reattachment of a single process. If the dtach process itself (the one managing the socket) dies, the attached program will lose its connection and likely terminate or become unmanageable through dtach. Unlike screen/tmux, dtach does not have built-in keybindings for detaching; detachment typically occurs when the controlling terminal exits.

HOW IT WORKS

dtach acts as a proxy between your program and your terminal. It sets up a Unix domain socket, launches your program, and redirects its standard input, output, and error streams through this socket. When you 'detach' (usually by closing the terminal), dtach continues to manage the program's I/O via the socket. When you 'attach', another dtach instance connects to the same socket, taking over the I/O.

TYPICAL USAGE

To start a new session named 'mysession' running bash:
dtach -c /tmp/mysession bash

To detach from this session (e.g., close the terminal window).

To reattach to the 'mysession' session from another terminal:
dtach -a /tmp/mysession

To terminate the session and the running program:
dtach -k /tmp/mysession

HISTORY

Developed as a minimalistic alternative to more feature-rich terminal multiplexers like GNU Screen, dtach emphasizes simplicity and a smaller footprint. Its design philosophy is to provide basic detachment and reattachment capabilities without the overhead of window management or complex features, making it ideal for specific, lightweight use cases where only process persistence is required.

SEE ALSO

screen(1), tmux(1), nohup(1), disown(1)

Copied to clipboard