#!/bin/bash LOG_FILE="/Users/mattbruce/.openclaw/workspace/logs/webapp-monitor.log" mkdir -p "$(dirname "$LOG_FILE")" # Function to get app name for port get_app_name() { case $1 in 3000) echo "gantt-board" ;; 3003) echo "blog-backup" ;; 3005) echo "heartbeat-monitor" ;; *) echo "unknown" ;; esac } # Function to get app directory for port get_app_dir() { case $1 in 3000) echo "/Users/mattbruce/Documents/Projects/OpenClaw/Web/gantt-board" ;; 3003) echo "/Users/mattbruce/Documents/Projects/OpenClaw/Web/blog-backup" ;; 3005) echo "/Users/mattbruce/Documents/Projects/OpenClaw/Web/heartbeat-monitor" ;; *) echo "" ;; esac } # Function to check if port is responding with HTTP 200 check_port() { local port=$1 local response response=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "http://localhost:$port" 2>/dev/null) if [ "$response" = "200" ]; then echo "up" else echo "down" fi } # Function to kill process on port kill_port() { local port=$1 pkill -f "--port $port" 2>/dev/null pkill -f ":$port" 2>/dev/null } # Function to restart app restart_app() { local port=$1 local dir=$2 cd "$dir" && nohup npm run dev -- --port $port > /dev/null 2>&1 & } echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting web app monitor check" >> "$LOG_FILE" restarted_any=false # Check each app for port in 3000 3003 3005; do status=$(check_port $port) app_name=$(get_app_name $port) if [ "$status" = "down" ]; then echo "[$(date '+%Y-%m-%d %H:%M:%S')] $app_name (port $port) is DOWN - restarting..." >> "$LOG_FILE" kill_port $port restarted_any=true else echo "[$(date '+%Y-%m-%d %H:%M:%S')] $app_name (port $port) is UP" >> "$LOG_FILE" fi done # Wait 2 seconds after kills, then restart down apps if [ "$restarted_any" = true ]; then sleep 2 for port in 3000 3003 3005; do status=$(check_port $port) if [ "$status" = "down" ]; then app_name=$(get_app_name $port) app_dir=$(get_app_dir $port) echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting $app_name on port $port..." >> "$LOG_FILE" restart_app $port "$app_dir" fi done # Wait 5 seconds for apps to start echo "[$(date '+%Y-%m-%d %H:%M:%S')] Waiting 5 seconds for apps to start..." >> "$LOG_FILE" sleep 5 fi # Final verification echo "[$(date '+%Y-%m-%d %H:%M:%S')] Final verification:" >> "$LOG_FILE" all_up=true for port in 3000 3003 3005; do status=$(check_port $port) app_name=$(get_app_name $port) if [ "$status" = "up" ]; then echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✓ $app_name (port $port) - UP" >> "$LOG_FILE" else echo "[$(date '+%Y-%m-%d %H:%M:%S')] ✗ $app_name (port $port) - STILL DOWN" >> "$LOG_FILE" all_up=false fi done if [ "$all_up" = true ]; then echo "[$(date '+%Y-%m-%d %H:%M:%S')] All web apps are running" >> "$LOG_FILE" else echo "[$(date '+%Y-%m-%d %H:%M:%S')] WARNING: Some web apps failed to start" >> "$LOG_FILE" fi echo "[$(date '+%Y-%m-%d %H:%M:%S')] Monitor check complete" >> "$LOG_FILE" echo "---" >> "$LOG_FILE"