openclaw/scripts/gateway-start.sh
valtterimelkko dd7f826d0a Add PM2-native health monitoring and startup improvements
- Created scripts/gateway-start.sh: Startup wrapper that cleans stale lock files
  before starting the gateway (prevents "already running" errors)

- Created scripts/pm2-health-monitor.js: Standalone health check process managed by PM2
  * Monitors port 18789 connectivity every 5 minutes
  * Detects unresponsive gateway (process running but port hung)
  * Force-restarts via killall + PM2 auto-recovery
  * Monitors inotify watcher usage (warns at 80% of limit)
  * Logs to /tmp/moltbot/pm2-health-monitor.log

- Updated ecosystem.config.cjs to:
  * Use gateway-start.sh wrapper for lock cleanup
  * Add moltbot-health-monitor as separate PM2 app
  * Health monitor runs alongside gateway (same PM2 daemon, isolated from other daemons)

Key Design Principles:
- PM2 handles process lifecycle (restart, memory limits, crash recovery)
- Health monitor adds responsiveness detection (what PM2 can't do alone)
- No systemd involvement (prevents port conflicts with other PM2 instances)
- Each PM2 daemon isolated: moltbot-gateway, si_project/dashboard, ai_product_visualizer

This ensures gateway remains stable even if it becomes unresponsive to Telegram messages.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-01-29 20:03:06 +00:00

33 lines
847 B
Bash
Executable File

#!/bin/bash
# Gateway startup wrapper
# Cleans stale lock files before starting the gateway
# This is used by PM2 as a pre-startup hook
set -e
LOCK_FILES=(
"$HOME/.clawdbot/gateway.lock"
"$HOME/.clawdbot/moltbot.lock"
"/tmp/moltbot-gateway.lock"
)
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Cleaning stale lock files..."
for lock_file in "${LOCK_FILES[@]}"; do
if [ -f "$lock_file" ]; then
file_age=$(($(date +%s) - $(stat -c%Y "$lock_file" 2>/dev/null || stat -f%m "$lock_file" 2>/dev/null || echo 0)))
# Clean if older than 5 minutes
if [ "$file_age" -gt 300 ]; then
rm -f "$lock_file" 2>/dev/null && echo " Removed: $lock_file"
fi
fi
done
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Lock cleanup complete. Starting gateway..."
# Start the actual gateway
cd /root/moltbot
exec node dist/entry.js gateway --port 18789