#!/usr/bin/env bash # Usage: scripts/run.sh start|status|stop|logs|tail (prefix with M4_SMOKE=1 for the smoke run) # The runner is detached (setsid, own process group) so it survives SSH disconnects; "start" again resumes. set -euo pipefail cd "$(dirname "$0")/.." PY="$PWD/.venv/bin/python" W=work; [ "${M4_SMOKE:-0}" = "1" ] && W=work_smoke mkdir -p "$W/logs" "$W/state" PIDF="$W/state/pipeline.pid" alive() { [ -f "$PIDF" ] && kill -0 "$(cut -d' ' -f1 "$PIDF")" 2>/dev/null; } case "${1:-}" in start) if alive; then echo "already running (pid $(cut -d' ' -f1 "$PIDF"))"; exit 0; fi setsid -f "$PY" -u src/run_pipeline.py "${@:2}" >> "$W/logs/pipeline.out" 2>&1 < /dev/null sleep 3; alive && echo "started pid $(cut -d' ' -f1 "$PIDF")" || { echo "failed to start"; tail -5 "$W/logs/pipeline.out"; } ;; status) alive && echo "RUNNING" || echo "not running"; (cd src && "$PY" run_pipeline.py --status) ;; stop) alive && kill -- -"$(cut -d' ' -f2 "$PIDF")" && echo stopped || echo "not running" ;; logs) tail -n "${2:-40}" "$W/logs/pipeline.out" ;; tail) tail -n "${3:-40}" "$W/logs/$2.log" ;; *) echo "usage: $0 start|status|stop|logs|tail "; exit 1 ;; esac