diff --git a/bin/chromebox-watchdog.sh b/bin/chromebox-watchdog.sh index 4dd187c..8345399 100755 --- a/bin/chromebox-watchdog.sh +++ b/bin/chromebox-watchdog.sh @@ -75,12 +75,21 @@ if healthy; then exit 0 fi -log "unhealthy ($HEALTH_FAIL_REASON), relaunching chromebox" -# Kill-loop guard (2026-10-04): if a chromium for this profile launched <2 min -# ago it's probably still loading the page — killing it just restarts the -# loop (observed: 646 killed twice while the page was still loading). +# Relaunch-loop guard (2026-10-04): if the main browser for this profile +# launched <2 min ago it's probably still starting up (CDP not yet bound). +# Relaunching now would kill a healthy-but-slow cold start via netvm-chrome.sh +# and reset the startup clock every cycle (observed: opm piled up 5 chromiums +# because the guard only protected the kill step, not the relaunch). Skip the +# entire cycle instead. +# NOTE: match only the main browser process (--remote-debugging-port present, +# no --type= flag). Renderer/gpu children (--type=renderer etc.) start later +# than the main process and must not satisfy this check. recent_pid="" -for _pid in $(pgrep -f "chromium.*profiles/${PROFILE}/" 2>/dev/null); do +for _pid in $(pgrep -f "chromium.*--remote-debugging-port=${CDP_PORT}([[:space:]]|$)" 2>/dev/null); do + # Skip child processes (renderer, gpu, etc.) — only the main browser counts + if ps -o args= -p "$_pid" 2>/dev/null | grep -q -- "--type="; then + continue + fi _start=$(date -d "$(ps -o lstart= -p "$_pid" 2>/dev/null)" +%s 2>/dev/null || echo 0) _now=$(date +%s) if [ $(( _now - _start )) -lt 120 ] && [ "$_start" -gt 0 ]; then @@ -89,9 +98,10 @@ for _pid in $(pgrep -f "chromium.*profiles/${PROFILE}/" 2>/dev/null); do fi done if [ -n "$recent_pid" ]; then - log "browser launched recently (pid $recent_pid), skipping kill (probably still starting)" + log "browser launched recently (pid $recent_pid), skipping relaunch (probably still starting)" exit 0 fi +log "unhealthy ($HEALTH_FAIL_REASON), relaunching chromebox" # bracket trick so pkill never matches its own command line pat="profiles/${PROFILE:0:${#PROFILE}-1}[${PROFILE: -1}]/" pkill -f "chromium.*$pat" 2>/dev/null || true