Linux·6 min read·

tmux Essentials for Long-Running Sessions

Keep terminal sessions alive across disconnects with tmux: sessions, windows, panes, and persistence.

NB

Netbay Cloud Team

Netbay Engineering

On this page

Any developer who has run a build, a Tailwind watcher, or a database import over SSH knows the pain of a dropped connection killing the process. tmux, the terminal multiplexer, solves this by running your shell in a persistent server that survives disconnects. You can detach from a session, reconnect later, and find the process exactly where you left it.

Starting and Detaching

The core operations are starting a session, detaching, reattaching, and killing:

bash
tmux new -s work
tmux new -s web -d
tmux ls
tmux attach -t work
tmux kill-session -t work

tmux new -s work creates a session named work. -d starts it detached so it keeps running in the background. tmux attach -t work reattaches to the running session. This is the entire workflow for surviving SSH disconnects: start a session, work, detach with the prefix sequence, and reattach later.

The default prefix is Ctrl-b. Press Ctrl-b then d to detach. Ctrl-b then c creates a new window. Ctrl-b then n and p move between windows, and Ctrl-b then 0-9 jumps to a numbered window.

Windows and Panes

Windows are like tabbed terminal sessions within your tmux session. Panes split a window into regions:

bash
Ctrl-b c    create a new window
Ctrl-b ,    rename the current window
Ctrl-b %    split vertically into two panes
Ctrl-b "    split horizontally into two panes
Ctrl-b arrow  move between panes
Ctrl-b x    close the current pane

A common layout is one pane running a dev server, one pane running the build watcher, and one pane for ad-hoc commands. This keeps context visible without tab-switching.

Capture and copy inside tmux uses the prefix plus brackets: Ctrl-b [ enters copy mode for scrolling the buffer, then q exits.

Persistence for Long-Running Work

A typical detached workflow for a build that takes hours looks like this:

bash
tmux new -s build -d
tmux send-keys -t build "npm run build:prod" Enter
tmux attach -t build

The send-keys command injects a keystroke into a detached session, so you can start the build, detach, reconnect later, and read the full output in the scrollback buffer. This is far more robust than running the command with a simple background & and hoping it survives.

For provisioning scripts that must survive as well:

bash
tmux new -s deploy
./deploy.sh
Ctrl-b d
tmux attach -t deploy

Run the deploy in the session, detach, and come back hours later to confirm the output.

Long outputs scroll past the visible area. In copy mode you can page through the entire scrollback:

bash
Ctrl-b [
PageUp / PageDown  scroll
/ pattern          search in scrollback
q                exit copy mode

Search in scrollback is especially useful for locating a specific error in an hours-long log stream without re-running the command.

Configuration

Purists write a small ~/.tmux.conf for convenience:

ini
set -g mouse on
set -g base-index 1
set -g history-limit 10000
bind r source-file ~/.tmux.conf

set -g mouse on enables mouse scrolling and pane selection. base-index 1 numbers windows from 1 instead of 0. history-limit 10000 keeps 10,000 lines of scrollback per pane.

tmux Session Model server tmux daemon session new -s build window Ctrl-b c pane split long build running Ctrl-b d to detach connection drops process survives tmux attach -t build reconnect and continue

Takeaway

tmux turns flaky SSH connections from a business risk into a minor inconvenience. Sessions outlive the connection, so long builds and deploys keep running no matter what. Try tmux on your next long-running task on a Netbay Linux VPS at netbayhosts.in.

Keep reading

Follow along on a real VPS

Deploy Linux in under 60 seconds

These guides are written against Ubuntu, Debian, and RHEL-family images — the same ones on NetBay.

Deploy an instance