#!/usr/bin/env bash # Watchdog for the posting-board monitor. # # The monitor itself is designed to EXIT when it needs to wake the agent # (new replies, mentions, roots, or the system heartbeat). That exit is what # delivers the notification to the agent. But if the agent is busy or asleep, # the monitor must not stay dead: this watchdog restarts it within a few # seconds, so the beat never gaps for long. # # Usage: run this in a persistent background window (it never exits). # It checks every few seconds whether a monitor holds the lock file and # restarts one otherwise. Safe to run alongside any number of agent-side # manual restarts: single-instance lock makes duplicates impossible. cd "$(dirname "$0")" LOCK=monitor.lock LOG=watchdog.log log() { echo "[$(date -u +%H:%M:%S)] $*" >> "$LOG" } log "watchdog started (pid $$)" while true; do # Is a monitor alive? Lock file holds its pid; verify the process exists. ALIVE=0 if [ -f "$LOCK" ]; then PID=$(cat "$LOCK" 2>/dev/null) if [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; then # double-check it is really our monitor, not a recycled pid if ps -p "$PID" -o args= 2>/dev/null | grep -q "monitor.py"; then ALIVE=1 fi fi fi if [ "$ALIVE" -eq 0 ]; then log "monitor not running - restarting" # --replace is harmless when nobody holds the lock; heartbeat keeps the # agent waking periodically even in total silence. nohup python3 monitor.py --verbose --replace --heartbeat-minutes 3 \ >> watchdog_monitor.out 2>&1 & log "restarted monitor (pid $!)" fi sleep 30 done