← back to Small Business Builder
scripts/hawk-local.sh
43 lines
#!/usr/bin/env bash
# Local PM2 watchdog for smb-builder. Fires every 5 min via launchd.
# Modeled on site-factory/scripts/hawk-local.sh and ventura-corridor/scripts/hawk-local.sh.
#
# Daemon dead → pm2 resurrect. smb-builder not online → pm2 restart.
# Old hawk used `curl /healthz || pm2 restart` with /opt/homebrew/bin/pm2 (doesn't
# exist on this Mac) so it logged "No such file" and silently failed every fire.
set -uo pipefail
export PATH="/Users/macstudio3/.npm-global/bin:/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin"
PM2=/Users/macstudio3/.npm-global/bin/pm2
LOG=/Users/macstudio3/Projects/small-business-builder/logs/hawk.log
TS() { date -u +%FT%TZ; }
mkdir -p "$(dirname "$LOG")"
if ! JLIST=$("$PM2" jlist 2>/dev/null); then
echo "[$(TS)] DAEMON DOWN — running pm2 resurrect" >> "$LOG"
"$PM2" resurrect >> "$LOG" 2>&1
exit 0
fi
STATUS=$(printf '%s' "$JLIST" \
| /usr/bin/python3 -c '
import json, sys
try:
procs = json.load(sys.stdin)
except Exception:
sys.exit(0)
for p in procs:
if p.get("name") == "smb-builder":
print(p.get("pm2_env", {}).get("status", "missing"))
sys.exit(0)
print("missing")
')
if [ "$STATUS" = "online" ]; then
echo "[$(TS)] OK — smb-builder online" >> "$LOG"
else
echo "[$(TS)] smb-builder status=$STATUS — restarting" >> "$LOG"
"$PM2" restart smb-builder >> "$LOG" 2>&1
fi