Skip to content

Instantly share code, notes, and snippets.

@bradleygolden
Last active January 22, 2026 15:14
Show Gist options
  • Select an option

  • Save bradleygolden/39b1eddbe772feb6db1aeb11cfc40ef0 to your computer and use it in GitHub Desktop.

Select an option

Save bradleygolden/39b1eddbe772feb6db1aeb11cfc40ef0 to your computer and use it in GitHub Desktop.
Ralph with CC TUI in each iteration
#!/bin/bash
# Ralph Wiggum Loop - Standalone Claude Code TUI Runner
# Run this from your terminal - it will open/close Claude Code repeatedly
set -e
# Get the prompt from arguments
PROMPT="$*"
if [ -z "$PROMPT" ]; then
echo "Usage: ./ralph-loop.sh <prompt>"
echo " prompt: Text prompt (use @file.md to expand file contents)"
echo ""
echo "Example:"
echo " ./ralph-loop.sh 'Refactor this code'"
echo " ./ralph-loop.sh @my-prompt.md"
exit 1
fi
echo "====================================="
echo " RALPH WIGGUM LOOP"
echo " Standalone Claude Code TUI Runner"
echo "====================================="
echo "Prompt: ${PROMPT:0:100}$((${#PROMPT} > 100 && echo '...' || echo ''))"
echo ""
echo "Each iteration will:"
echo " 1. Open Claude Code TUI"
echo " 2. Execute your prompt"
echo " 3. Auto-close when complete"
echo " 4. Wait 3 seconds"
echo " 5. Repeat forever"
echo ""
echo "Press Ctrl+C to stop"
echo "====================================="
echo ""
sleep 2
# Counter for iterations
ITERATION=1
SESSION_NAME="ralph-loop-$$"
while true; do
clear
echo "====================================="
echo " ITERATION #$ITERATION"
echo "====================================="
date '+%Y-%m-%d %H:%M:%S'
echo ""
echo "Starting Claude Code TUI..."
echo "Will auto-close when idle (no output for 1m)"
echo ""
# Kill any existing session
tmux kill-session -t "$SESSION_NAME" 2>/dev/null || true
# Create tmux session and run claude in background
# Properly escape the prompt for tmux
tmux new-session -d -s "$SESSION_NAME" "claude \"$PROMPT\""
# Start idle monitor in background
(
LAST_ACTIVITY=$(date +%s)
LAST_BUFFER=""
while tmux has-session -t "$SESSION_NAME" 2>/dev/null; do
sleep 1
# Check buffer for changes
CURRENT_BUFFER=$(tmux capture-pane -t "$SESSION_NAME" -p 2>/dev/null || echo "")
if [ "$CURRENT_BUFFER" != "$LAST_BUFFER" ]; then
LAST_ACTIVITY=$(date +%s)
LAST_BUFFER="$CURRENT_BUFFER"
fi
# Check idle time
CURRENT_TIME=$(date +%s)
IDLE_TIME=$((CURRENT_TIME - LAST_ACTIVITY))
if [ "$IDLE_TIME" -ge 60 ]; then
# Send Ctrl+D to exit the TUI
tmux send-keys -t "$SESSION_NAME" C-d
sleep 1
tmux kill-session -t "$SESSION_NAME" 2>/dev/null
break
fi
done
) &
MONITOR_PID=$!
# Attach to the session (this blocks until session ends)
tmux attach-session -t "$SESSION_NAME"
# Clean up monitor
kill $MONITOR_PID 2>/dev/null || wait $MONITOR_PID 2>/dev/null
echo ""
echo "====================================="
echo " Iteration #$ITERATION complete"
echo "====================================="
echo "Waiting 3 seconds before next iteration..."
echo "(Press Ctrl+C to stop the loop)"
echo ""
sleep 3
ITERATION=$((ITERATION + 1))
done
# Cleanup on exit
trap "tmux kill-session -t '$SESSION_NAME' 2>/dev/null" EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment