tmux
Terminal multiplexer for background processes, output capture, and session management. Every process runs in a named session you can inspect later.
Running Background Processes
Start a command in a named detached session:
tmux new-session -d -s myserver 'python -m http.server 8080'
tmux new-session -d -s build -c /path/to/project 'make build'
Keep a session alive after the command completes:
tmux new-session -d -s task 'command; exec bash'
Only create if it doesn't exist:
tmux has -t myserver || tmux new-session -d -s myserver 'command'
Capturing Output
Get the output from a running session:
tmux capture-pane -t mysession -p # Visible output only
tmux capture-pane -t mysession -p -S - # Full scrollback history
tmux capture-pane -t mysession -p -S -100 # Last 100 lines
tmux capture-pane -t mysession -p > file.txt # Save to file
Sending Input
Send keystrokes to a session:
tmux send-keys -t mysession 'echo hello' Enter # Type and press Enter
tmux send-keys -t mysession C-c # Send Ctrl+C
Waiting for Completion
Signal when a job finishes, then wait for it:
tmux new-session -d -s job 'command; tmux wait-for -S job-done'
tmux wait-for job-done
Session Management
tmux ls # List all sessions
tmux kill-session -t myserver # Kill specific session
tmux kill-server # Kill all sessions
Common Patterns
Development servers:
tmux new-session -d -s backend 'bun run backend'
tmux new-session -d -s frontend 'bun run frontend'
Run and capture output in a script:
tmux new-session -d -s job 'command'
sleep 0.5
output=$(tmux capture-pane -t job -p)
echo "$output"
Tips
- Use
-c /pathto set working directory when creating a session - Use
exec bashto keep sessions alive after commands finish - Use
-S -withcapture-paneto get full scrollback history