File size: 712 Bytes
47ea602
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env bash
# Local: stop the pod (keeps its volume) when the RunPod balance drops below $MIN (default 6).
cd "$(dirname "$0")/.."
source scripts/pod.env
MIN=${MIN:-6}
while true; do
  OUT=$(nix shell nixpkgs#python3 -c python3 scripts/runpod.py balance 2>/dev/null) || { sleep 120; continue; }
  BAL=$(echo "$OUT" | grep clientBalance | grep -o '[0-9.]*')
  RUN=$(echo "$OUT" | grep -c '"RUNNING"')
  echo "$(date +%T) balance=$BAL running=$RUN"
  [[ "$RUN" == 0 ]] && { echo "no running pod, exit"; exit 0; }
  if awk "BEGIN{exit !($BAL < $MIN)}"; then
    echo "balance below $MIN, stopping $POD_ID"; nix shell nixpkgs#python3 -c python3 scripts/runpod.py stop "$POD_ID"; exit 0
  fi
  sleep 300
done