#!/bin/bash
# Interrupts the video-lab ComfyUI (:8192 only) if MemAvailable drops under 5 GB. Unified-memory OOM freezes the Spark.
# usage: memguard.sh <stop-file>   (exits when the stop file appears)
while [ ! -f "$1" ]; do
  a=$(awk "/MemAvailable/ {print int(\$2/1048576)}" /proc/meminfo)
  if [ "$a" -lt 5 ]; then
    curl -s -m 5 -X POST 127.0.0.1:8192/interrupt >/dev/null
    echo "$(date +%T) MemAvailable=${a}G -> interrupted :8192" >> logs/memguard.log
    sleep 20
  fi
  sleep 2
done
