cmd/sgai/skel/.sgai/skills/run-long-running-processes-in-tmux/SKILL.md
Guide for using tmux to manage detached sessions for long-running processes, including lifecycle management, cleanup, startup, verification, and output capture; When you need to run servers or long-running commands in the background, send commands to them, capture output, handle TTY requirements, or manage session lifecycle with error handling
npx skillsauth add sandgardenhq/sgai run-long-running-processes-in-tmuxInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Tmux allows running processes in detached sessions, providing pseudo-terminals for processes requiring TTY, and tools to interact with them without attaching. Enhanced with complete session lifecycle management including cleanup, startup verification, and output capture.
Use when:
| Command | Purpose | Example | |---------|---------|---------| | kill-session -t name 2>/dev/null || true | Cleanup existing | tmux kill-session -t server 2>/dev/null || true | | new-session -d -s name "cmd" | Start detached | tmux new-session -d -s server "python -m http.server" | | capture-pane -t name -S - -p | Verify startup | tmux capture-pane -t server -S - -p | grep -q "Serving" | | send-keys -t name "cmd" C-m | Send command | tmux send-keys -t server "echo test" C-m | | capture-pane -t name -S - -p > file | Capture output | tmux capture-pane -t server -S - -p > log.txt | | attach -t name | Reattach | tmux attach -t server | | kill-session -t name | Stop session | tmux kill-session -t server |
# Complete lifecycle with error handling
SESSION_NAME="myserver"
COMMAND="python -m http.server 8000"
# 1. Cleanup existing session
tmux kill-session -t "$SESSION_NAME" 2>/dev/null || true
# 2. Start new session
tmux new-session -d -s "$SESSION_NAME" "$COMMAND"
# 3. Wait for startup and verify
sleep 2
if ! tmux capture-pane -t "$SESSION_NAME" -S - -p | grep -q "Serving HTTP"; then
echo "Server failed to start properly"
tmux kill-session -t "$SESSION_NAME"
exit 1
fi
# 4. Capture initial output
tmux capture-pane -t "$SESSION_NAME" -S - -p > startup.log
# 5. Session is ready for interaction
# Basic detached session
tmux new-session -d -s mysession "my_long_running_command"
# With working directory and logging
tmux new-session -d -s appsession -c /path/to/dir "./run_app.sh >app.log 2>&1"
# With session conflict handling
if tmux has-session -t appsession 2>/dev/null; then
echo "Session already exists, cleaning up..."
tmux kill-session -t appsession
fi
tmux new-session -d -s appsession "./run_app.sh"
For processes requiring TTY, tmux provides PTYs automatically.
# Check if session exists
if tmux has-session -t mysession 2>/dev/null; then
echo "Session is running"
else
echo "Session not found"
fi
# Verify process output
tmux capture-pane -t mysession -S - -p | grep -q "expected_pattern"
echo $?
# Check session status
tmux display-message -t mysession -p "#{session_name}: #{window_status}"
# Send command with enter
tmux send-keys -t mysess:0.0 "cd /path/to/dir" C-m
tmux send-keys -t mysess:0.0 "./run_my_task.sh --option" C-m
# Send with delay for process readiness
tmux send-keys -t server "status" C-m
sleep 1
tmux capture-pane -t server -S - -p | tail -5
Note: Process must accept stdin as commands. May need delays for shell readiness.
# Capture full history
tmux capture-pane -t mysess:0.0 -S - -p > output.txt
# Capture with verification
OUTPUT=$(tmux capture-pane -t mysess:0.0 -S - -p)
if echo "$OUTPUT" | grep -q "ERROR"; then
echo "Error detected in output"
fi
# Alternative with buffer
tmux capture-pane -t mysess:0.0 -S -
tmux save-buffer -b 0 output.txt
# Real-time monitoring
watch -n 5 'tmux capture-pane -t server -S - -p | tail -10'
Text only, may lose colors unless -e flag used.
Enables background execution of servers, automated testing, and remote process management without keeping terminals open. Enhanced lifecycle management reduces repetitive session management tasks and provides reliable server testing workflows with proper error handling and cleanup.
documentation
Start, stop, and steer agentic sessions in sgai workspaces. Use when you need to launch AI agent sessions, halt running sessions, or inject steering instructions to guide the agent mid-execution without stopping it.
development
Monitor sgai workspace status, events, progress, diffs, and workflow diagrams. Use when you need to observe what agents are doing, track progress, get the current state of all workspaces, subscribe to real-time updates via SSE, or inspect code changes.
development
Access agents, skills, and code snippets available in sgai workspaces. Use when you need to discover what agents are defined in a workspace, browse available skills, get skill instructions, find code snippets by language, or retrieve snippet content for a specific task.
data-ai
Handle agent questions and work gates in sgai workspaces. Use when an agent is blocked waiting for human input, when you need to respond to multi-choice questions, approve work gates, or provide free-text answers to agent queries.