test-repo/scripts/monitor-web-apps.sh
Matt Bruce b934c9fdb3 Task #7: Root cause analysis - why websites die
- Analyzed system limits, memory usage, process status
- Identified primary suspect: Next.js dev server memory leaks
- Secondary suspects: macOS power mgmt, SSH timeout, OOM killer
- Created monitoring script for CPU/memory/file descriptors
- Documented recommendations: production builds, PM2, nohup
2026-02-18 16:04:44 -06:00

123 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
# Web App Monitor - Auto Restart Script
# Ports: 3000 (gantt-board), 3003 (blog-backup), 3005 (heartbeat-monitor)
PORTS=(3000 3003 3005)
PROJECTS=(
"/Users/mattbruce/Documents/Projects/OpenClaw/Web/gantt-board"
"/Users/mattbruce/Documents/Projects/OpenClaw/Web/blog-backup"
"/Users/mattbruce/Documents/Projects/OpenClaw/Web/heartbeat-monitor"
)
NAMES=("gantt-board" "blog-backup" "heartbeat-monitor")
RESTARTED=()
LOG=""
log() {
LOG+="$1\n"
echo -e "$1"
}
check_port() {
local port=$1
local timeout=5
local response
response=$(curl -s -o /dev/null -w "%{http_code}" --max-time $timeout "http://localhost:$port" 2>/dev/null)
echo "$response"
}
restart_app() {
local port=$1
local project=$2
local name=$3
log " ↻ Restarting $name on port $port..."
# Kill any process using the port
pkill -f ":$port" 2>/dev/null
sleep 2
# Start the app in background
cd "$project" && npm run dev -- --port $port > /dev/null 2>&1 &
RESTARTED+=("$name (port $port)")
}
log "═══════════════════════════════════════════════════"
log "🌐 Web App Monitor - $(date)"
log "═══════════════════════════════════════════════════"
log ""
# Check all ports
for i in "${!PORTS[@]}"; do
PORT="${PORTS[$i]}"
PROJECT="${PROJECTS[$i]}"
NAME="${NAMES[$i]}"
log "📡 Checking $NAME on port $PORT..."
STATUS=$(check_port $PORT)
if [ "$STATUS" = "200" ]; then
log " ✅ Healthy (HTTP $STATUS)"
else
if [ -z "$STATUS" ]; then
log " ❌ No response (timeout/connection refused)"
else
log " ❌ Unhealthy (HTTP $STATUS)"
fi
restart_app $PORT "$PROJECT" "$NAME"
fi
log ""
done
# Wait for restarts to come up
if [ ${#RESTARTED[@]} -gt 0 ]; then
log "⏳ Waiting 5 seconds for restarts to initialize..."
sleep 5
log ""
fi
# Final verification
log "═══════════════════════════════════════════════════"
log "📊 Final Verification"
log "═══════════════════════════════════════════════════"
ALL_HEALTHY=true
for i in "${!PORTS[@]}"; do
PORT="${PORTS[$i]}"
NAME="${NAMES[$i]}"
STATUS=$(check_port $PORT)
if [ "$STATUS" = "200" ]; then
log "$NAME (port $PORT): HTTP 200"
else
log "$NAME (port $PORT): Failed (HTTP ${STATUS:-'no response'})"
ALL_HEALTHY=false
fi
done
log ""
log "═══════════════════════════════════════════════════"
log "📋 Summary"
log "═══════════════════════════════════════════════════"
if [ ${#RESTARTED[@]} -eq 0 ]; then
log " 📍 All apps were already running and healthy"
else
log " 🔄 Restarted apps:"
for app in "${RESTARTED[@]}"; do
log "$app"
done
fi
log ""
if [ "$ALL_HEALTHY" = true ]; then
log " 🎯 Final Status: All apps responding with HTTP 200"
else
log " ⚠️ Final Status: Some apps are not responding"
fi
log ""
log "═══════════════════════════════════════════════════"