#!/usr/bin/env bash # # Keep-alive supervisor for the satellite process. # # Bare `nohup bun satellite/dist/satellite.js` cannot come back after the # process exits, so a hot-deploy that swaps the bundle and exits would leave the # node dead. This loop relaunches the satellite whenever it exits, which is # exactly how a deploy restarts onto new code: the /sat/admin/deploy handler # swaps dist/satellite.js then calls process.exit(0), and this loop re-launches # it on the fresh bundle. # # Rollout (one-time, per node): # 1. Sync the repo (or at least scripts/ + satellite/dist/) to the node. # 2. Stop the old bare-nohup satellite: pkill -f 'satellite/dist/satellite.js' || true # 3. Start under this loop: nohup ./scripts/run-satellite.sh > ~/satellite.log 2>&1 & # # Set SATELLITE_ENTRY to run from source instead of the bundle (dev nodes): # SATELLITE_ENTRY=satellite/src/index.ts ./scripts/run-satellite.sh # set -uo pipefail cd "$(dirname "$0")/.." export PATH="/opt/homebrew/bin:$HOME/.bun/bin:$PATH" set -a; [ -f .env ] && source .env; set +a # Hold a display power assertion for as long as this supervisor lives. # # The satellite drives a real macOS GUI app and captures evidence with # `catuse getscreenshot`, so a screen saver turns every screenshot into a picture # of the screen saver. Setting the screensaver idleTime to 0 does NOT work on # these nodes: MDM ships /Library/Managed Preferences/com.apple.screensaver.plist # (idleTime=300, askForPassword=true) and a managed preference outranks the # user's ByHost value, so loginwindow keeps reading 300 no matter what # `defaults -currentHost write` puts on disk. A power assertion sidesteps the # preference layer entirely — it is the same mechanism that keeps the screen # saver away while a video player is in the foreground. # # `-w $$` releases the assertion when this supervisor exits, so a stopped node # never leaves one behind. Re-armed per loop iteration in case it ever dies. CAFFEINATE_PID="" ensure_awake() { # `kill -0 0` targets the whole process group and always succeeds, so an empty # CAFFEINATE_PID must be tested separately or the assertion is never taken. if [ -z "${CAFFEINATE_PID}" ] || ! kill -0 "${CAFFEINATE_PID}" 2>/dev/null; then caffeinate -dims -w $$ & CAFFEINATE_PID=$! echo "[run-satellite] display assertion held by caffeinate ${CAFFEINATE_PID}" fi } ensure_awake ENTRY="${SATELLITE_ENTRY:-satellite/dist/satellite.js}" BACKOFF="${SATELLITE_RESTART_SECONDS:-2}" echo "[run-satellite] supervising ${ENTRY} (restart backoff ${BACKOFF}s)" while true; do ensure_awake bun "${ENTRY}" code=$? echo "[run-satellite] satellite exited (code ${code}); restarting in ${BACKOFF}s" sleep "${BACKOFF}" done