← topics

--- title: Heartbeat Monitor Architecture updated: 2026-06-11 ---

Heartbeat Monitor Architecture

Overview

The heartbeat monitor checks every local service every 60 seconds via a launchd-triggered shell script. It detects dead ports, unresponsive health endpoints, CLOSE_WAIT socket leaks, and CPU spikes, then auto-restarts the affected service.

Script

Location: `projects/heartbeat-monitor/heartbeat.sh` Launchd label: `com.randy.heartbeat-monitor` Interval: 60s Log: `projects/heartbeat-monitor/heartbeat.log`

Services Monitored

ServicePortHealth URLRestart Method

Portal Auth5190`/healthz``launchctl kickstart`
Mission Control5173`/api/status``launchctl kickstart`
Document Finder8093`/health``launchctl kickstart`
Cost Guard7720`/health``launchctl kickstart`
YouTube Synopsis5182`/``launchctl kickstart`
Video Analysis5191`/health``nohup node`
X Briefing5181`/``launchctl kickstart`
Market Research8095`/mr/status``nohup node`
Filing System API8091`/health``launchctl kickstart`
MUE API7710`/health``launchctl kickstart`

Recent Fix (2026-06-11)

Problem: Script silently stopped checking all but portal-auth

Root cause: `set -euo pipefail` combined with `lsof` exiting code 1 when a port has no listener. After portal-auth passed, the next service's `lsof` call killed the script with no visible error (logged only to launchd stderr).

Fix: Removed `set -e`. All `lsof` calls wrapped with `|| true` to handle normal "port not in use" exit code.

Architecture

``` launchd (every 60s) │ └── heartbeat.sh │ ├── lsof :port → check listening ├── curl /health → check responsive ├── check_stuck() → detect CLOSE_WAIT leaks └── check_cpu() → detect CPU spikes │ └── launchctl kickstart service (if unhealthy) ```