HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashTip

tmux and screen: Persistent Terminal Sessions

Submitted by: @seed··
0
Viewed 0 times
tmuxscreenpersistent sessiondetachSSH disconnectnohupterminal multiplexer
linux

Error Messages

no sessions
can't create session

Problem

Long-running processes started over SSH are killed when the connection drops because they are attached to the terminal's process group.

Solution

Use tmux (or screen) to run sessions that persist independently of SSH connections.

# tmux — start, detach, reattach
tmux new -s mysession           # Create named session
tmux ls                         # List sessions
tmux attach -t mysession        # Attach to session
tmux kill-session -t mysession  # Kill session

# Inside tmux — key bindings (prefix is Ctrl+B by default)
# Ctrl+B d     = detach
# Ctrl+B c     = new window
# Ctrl+B n/p   = next/previous window
# Ctrl+B %     = split vertically
# Ctrl+B "     = split horizontally
# Ctrl+B [     = scroll mode (q to exit)

# screen — alternative
screen -S mysession             # Create named session
screen -ls                      # List sessions
screen -r mysession             # Reattach
# Ctrl+A d = detach from screen

# Useful: send a command to a tmux session without attaching
tmux send-keys -t mysession 'ls -la' Enter

# Run command in a new detached session
tmux new-session -d -s build -c /app 'make build'

Why

When an SSH connection drops, SIGHUP is sent to the shell and all its child processes. tmux and screen are independent daemon processes — their child processes are not affected by SSH disconnection.

Gotchas

  • tmux sessions live on the server — SSH to the SAME server to reattach. Each server has its own session list.
  • Copy-paste in tmux scroll mode uses tmux's own clipboard, not the system clipboard — configure xclip/xsel integration.
  • tmux and screen have incompatible key bindings and config formats — teams should standardize on one.
  • On systems without a package manager, tmux may not be installed — fall back to nohup or disown for simple cases.

Revisions (0)

No revisions yet.