Keeping Long‑Running Shell Work Alive with Detached Tmux Sessions
Run commands in a named tmux session so an SSH drop or laptop sleep does not kill the shell; you can reattach later and see the same scrollback and running processes.
16 Sept 2026, 08:43 UTC

Problem and Desired Outcome
When you start a long‑running command over SSH, a network interruption or a laptop going to sleep can terminate the shell and any processes it launched. The goal is to keep the command alive in a tmux session that survives disconnects, letting you reattach later and continue exactly where you left off.
Prerequisites
First, install tmux from your operating system’s package manager. Verify the installation with tmux -V; any version 2.6 or newer supports the features described. Ensure your terminal sets a sane TERM variable (typically xterm-256color) so tmux can render correctly. No root privileges are required for per‑user sessions.
Creating a Detached Named Session
Use the new-session command with the -d flag to start detached, and -s to give the session a meaningful name, for example work:
tmux new-session -d -s work
This creates a tmux server (if none exists) and a session called work that has no attached client. The session remains active as long as the tmux server process lives.
Starting the Long‑Running Job
Within the detached session, launch the command you want to keep alive. A simple test is the sleep utility:
tmux send-keys -t work 'sleep 300' C-m
The send-keys command targets the session work and sends the keystrokes followed by a carriage return, effectively typing sleep 300 and pressing Enter. The job now runs in its own pane.
Detaching the Session
To leave the session without killing the job, use tmux’s prefix key sequence: press Ctrl‑b, release, then press d. This is equivalent to the client command tmux detach-client. After detaching, you can close the terminal, suspend your laptop, or lose the SSH connection; the work session continues running.
Reattaching Later
When you have a new terminal or need to resume work, attach to the session with:
tmux attach -t work
You will see the same pane, the same scrollback history, and the sleep process still active.
Checking That the Session Is Alive
Run tmux ls to list all sessions. An attached session shows a * before its name and a count of attached clients:
tmux ls
# example output
# work tmux: 1 windows (created Thu Nov 12 00:15:00 2026) [80×24] (attached)
To verify the pane’s process is still running, use:
tmux list-panes -t work -F '#{pane_pid} #{pane_current_command}'
The output should include the PID of the sleep process and the command name, confirming it has not terminated.
Minimal Configuration for Persistence
Add the following lines to your ~/.tmux.conf to improve the experience:
# Keep a large scrollback buffer
set -g history-limit 50000
# Enable mouse support for easier pane selection
set -g mouse on
Reload the configuration in a test session with tmux source-file ~/.tmux.conf or simply restart the tmux server (which ends all sessions, so avoid this unless you intend to kill everything). Verify that the options are active:
tmux show-options -g # shows global options
tmux show-options -gw # shows server‑wide options
If the output lists the values you set, the configuration is applied correctly.
To apply the new options without restarting tmux, you can reload the configuration in an existing session using tmux source-file ~/.tmux.conf. This command reads the file and applies any new settings to the current server, so you do not lose your attached clients.
If you work over a non‑standard socket, you can specify the path with tmux -S /tmp/my_socket, but the basic workflow remains the same.
From inside the session you can also run tmux detach-client to leave without using the prefix.
Recovery After a Dropped SSH Connection
If you try to attach and receive an error such as no such session, first list sessions with tmux ls. The session may still exist if the tmux server survived the disconnect; in that case, simply re‑attach. If the server process itself died (e.g., the host rebooted or the tmux server was killed), the child processes are gone, and you must restart the job. In that case, recreate the session and rerun the command.
Handling a Stuck Pane
Sometimes a pane may appear unresponsive. You can terminate a single pane with:
tmux kill-pane -t work:0.0
To end the entire session (and all its processes) safely, use:
tmux kill-session -t work
Only run the latter when you are certain the processes inside are no longer needed, as it stops everything inside the session.
Cautions and Limitations
The tmux server must stay alive; it does not survive a full system reboot, container stop, or out‑of‑memory kill. Also, option names and scopes are version‑sensitive; always check tmux show-options before assuming a config line works on your version.
Running tmux kill-server ends every session and all child processes, so avoid using it as a cleanup step unless you truly want to terminate everything.
Multiple clients can attach to the same session, but the layout follows the smallest terminal size unless you configure resize behavior. Expect differences in pane dimensions when connecting from very different terminal types.
If you run tmux inside another terminal multiplexer such as screen or a nested tmux instance, the prefix key may be intercepted. Send the inner prefix explicitly (e.g., Ctrl‑b b) or use tmux detach-client from inside the session.
Verification Checklist
- Run tmux -V and tmux ls before creating the session; note the session name.
- Start a harmless job like sleep 300 inside the session.
- Detach with Ctrl‑b d or tmux detach-client.
- Close the terminal or suspend the laptop.
- Open a new terminal and run tmux attach -t work.
- Confirm the pane still reports sleep via tmux display-message -p -t work '#{pane_current_command}'.
- Enter copy mode (Ctrl‑b [) and scroll up to verify that previous output is present; increase history-limit if missing.
- Inspect the config with tmux show-options -g and tmux show-options -gw to ensure your settings took effect.
Conclusion
Using a detached, named tmux session gives you a reliable way to keep long‑running shell work alive across disconnects, reboots of the terminal, or brief laptop sleep periods. By following the simple steps—install tmux, create a named detached session, launch your command, detach, and reattach—you can maintain context, scrollback, and process state without manual re‑execution. Pair the workflow with a modest .tmux.conf tweak to enlarge scrollback and enable mouse support, and you have a portable, low‑overhead solution for any developer who needs persistent terminal sessions.