How to respawn panes and manage process exit behavior?

Quick Answer

Use respawn-pane to restart commands in existing panes and remain-on-exit to keep panes open when processes exit, making debugging and process management easier.

Respawn-Pane Usage

Basic respawn commands:

# Restart the same command in current pane
tmux respawn-pane

# Restart with a new command
tmux respawn-pane 'htop'

# Kill existing process and restart
tmux respawn-pane -k 'npm run dev'

# Restart specific pane
tmux respawn-pane -t 0 'python server.py'

Remain-on-exit configuration:

# Keep panes open when processes exit
set-window-option remain-on-exit on

# Set for all new windows globally
set-option -g remain-on-exit on

# Enable for current window only
tmux setw remain-on-exit on

# Disable remain-on-exit
tmux setw remain-on-exit off

Working with dead panes:

# List panes and their status
tmux list-panes -F '#{pane_index}: #{pane_current_command} #{?pane_dead,[DEAD],}'

# Check if pane is dead
tmux display -t 0 '#{?pane_dead,Dead,Alive}'

# Respawn all dead panes in window
tmux list-panes -F '#{pane_index}' | \
  xargs -I {} sh -c 'tmux display -t {} "#{?pane_dead,{},}" | \
  grep -q "^[0-9]" && tmux respawn-pane -t {}'

Development workflow examples:

# Setup development environment with remain-on-exit
tmux new-session -d -s dev
tmux setw remain-on-exit on

# Frontend dev server (pane 0)
tmux send-keys 'npm run dev' C-m

# Backend server (pane 1)  
tmux split-window -h
tmux send-keys 'python manage.py runserver' C-m

# Test runner (pane 2)
tmux split-window -v
tmux send-keys 'npm run test:watch' C-m

# When servers crash, they stay visible for debugging
# Restart with: tmux respawn-pane -t pane_number

Key bindings for quick respawn:

# Add to ~/.tmux.conf
# Restart current pane
bind-key r respawn-pane -k

# Restart pane with confirmation
bind-key R confirm-before -p "Restart pane? (y/n)" "respawn-pane -k"

# Toggle remain-on-exit for current window
bind-key T if-shell 'tmux showw -v remain-on-exit | grep -q on' \
  'setw remain-on-exit off; display "remain-on-exit: OFF"' \
  'setw remain-on-exit on; display "remain-on-exit: ON"'

# Respawn with new command prompt
bind-key C-r command-prompt -p "Restart with command:" "respawn-pane '%%'"

Monitoring and automation:

# Auto-restart script for critical services
#!/bin/bash
while true; do
  sleep 30
  # Check if service pane is dead and restart
  if tmux display -t service '#{?pane_dead,1,0}' | grep -q 1; then
    echo "Service died, restarting..."
    tmux respawn-pane -t service 'python critical_service.py'
    tmux display-message "Service restarted at $(date)"
  fi
done

# Hook to auto-restart on exit
set-hook -g pane-died 'if -F "#{==:#{pane_current_command},python}" "respawn-pane"'

Process management patterns:

# Check exit codes before respawning
tmux display -t 0 '#{pane_dead_status}'

# Conditional respawn based on exit code
if [ "$(tmux display -t 0 -p '#{pane_dead_status}')" = "0" ]; then
  echo "Process exited successfully"
else
  echo "Process crashed, restarting..."
  tmux respawn-pane -t 0
fi

# Log exit information before respawn
tmux respawn-pane -t server \
  'echo "Restarted at $(date)" >> restart.log; ./start_server.sh'

Debugging Workflows

Common patterns for debugging with respawn-pane:

  • Crash analysis: Keep crashed processes visible to read error messages
  • Exit code checking: Use pane_dead_status to check why process exited
  • Quick restart: Bound keys for immediate process restart
  • Service monitoring: Auto-restart critical services on failure