are_games_running.sh
· 832 B · Bash
Sin formato
#!/usr/bin/env bash
set -euo pipefail
POWER_LIMIT=30
power="$(nvidia-smi --query-gpu=power.draw --format=csv,noheader,nounits 2>/dev/null | head -n1)"
if [[ -z "$power" ]]; then
echo "OOPS: cannot read GPU power - check nvidia GPU driver"
exit 1
fi
# Numeric compare (decimal-safe)
if awk -v p="$power" -v t="$POWER_LIMIT" 'BEGIN { exit !(p < t) }'; then
echo "Ignoring low GPU power ${power} W < ${POWER_LIMIT} W"
exit 1
else
echo "GPU Power is $power W -- above idle limit ($POWER_LIMIT W)"
fi
# protect process containing one of these strings
NOT_GAMES=(
blender
dontkillme
)
for s in "${NOT_GAMES[@]}"; do
if ps -eo args \
| grep -qi -- "$s" \
| grep -qv grep; then
echo "PROTECTED process matched, exiting '$s'"
exit 1
fi
done
echo "Games likely running with GPU ${power}W"
exit 0
| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | POWER_LIMIT=30 |
| 5 | |
| 6 | power="$(nvidia-smi --query-gpu=power.draw --format=csv,noheader,nounits 2>/dev/null | head -n1)" |
| 7 | |
| 8 | if [[ -z "$power" ]]; then |
| 9 | echo "OOPS: cannot read GPU power - check nvidia GPU driver" |
| 10 | exit 1 |
| 11 | fi |
| 12 | |
| 13 | |
| 14 | # Numeric compare (decimal-safe) |
| 15 | if awk -v p="$power" -v t="$POWER_LIMIT" 'BEGIN { exit !(p < t) }'; then |
| 16 | echo "Ignoring low GPU power ${power} W < ${POWER_LIMIT} W" |
| 17 | exit 1 |
| 18 | else |
| 19 | echo "GPU Power is $power W -- above idle limit ($POWER_LIMIT W)" |
| 20 | fi |
| 21 | |
| 22 | # protect process containing one of these strings |
| 23 | NOT_GAMES=( |
| 24 | blender |
| 25 | dontkillme |
| 26 | ) |
| 27 | |
| 28 | for s in "${NOT_GAMES[@]}"; do |
| 29 | if ps -eo args \ |
| 30 | | grep -qi -- "$s" \ |
| 31 | | grep -qv grep; then |
| 32 | echo "PROTECTED process matched, exiting '$s'" |
| 33 | exit 1 |
| 34 | fi |
| 35 | done |
| 36 | |
| 37 | |
| 38 | echo "Games likely running with GPU ${power}W" |
| 39 | exit 0 |
| 40 |