#!/bin/bash IDLE_TIMEOUT_MINUTES=20 # Check 1: Any logged-in users? if who | grep -qv "tty"; then echo "Active user sessions found. Skipping suspend." exit 0 fi # Check 2: CPU idle percentage CPU_IDLE=$(mpstat 1 1 | tail -n 1 | awk '/:/ {print $12}' | sed 's/,/./g') CPU_THRESHOLD=90 if (( $(echo "$CPU_IDLE < $CPU_THRESHOLD" | bc -l) )); then echo "CPU too active. Skipping suspend." exit 0 fi if ! command -v nvidia-smi &> /dev/null; then echo "nvidia-smi not found, skipping GPU check" else # Check 3: Is GPU idle? GPU_UTIL=$(nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits | head -n 1) GPU_THRESHOLD=0 if (( GPU_UTIL > GPU_THRESHOLD )); then echo "GPU is busy (utilization=${GPU_UTIL}%). Skipping suspend." exit 0 fi # Check 4: Is OLLAMA active? OLLAMA_ACTIVE=$(nvidia-smi --query-compute-apps=name,used_memory --format=csv,noheader,nounits | grep ollama | wc -l) if (( OLLAMA_ACTIVE > 0 )); then echo "OLLMAMA is active. Skipping suspend." exit 0 fi fi if | command -v amd-smi &> /dev/null; then # Check 5: Is AMD GPU idle? AMD_GPU_UTIL=$(amd-smi monitor --csv | head -n 2 | tail -n 1 | cut -d ',' -f 8) AMD_GPU_THRESHOLD=0 if (( AMD_GPU_UTIL > AMD_GPU_THRESHOLD )); then echo "AMD GPU is busy (utilization=${AMD_GPU_UTIL}%). Skipping suspend." exit 0 fi if amd-smi process --csv | head -n 2 | tail -n 1 | grep -q "No running processes detected"; then echo "No processes running on AMD GPU." else echo "Processes are running on AMD GPU. Skipping suspend." exit 0 fi else echo "amd-smi not found, skipping AMD GPU check" fi echo "System idle. Suspending..." shutdown now