用于 Linux 服务器风扇控制, 经常觉得服务器显卡的风扇调度太保守了, 没事就想摸摸看, 然后核心温度才50度, 摸上去都有70多度了, 因此想要自己控制显卡的风扇转速, 狠狠地转! 有时候核心都快70度了, 风扇转速才70%。

#!/bin/bash

gpu_index_str=$(nvidia-smi --query-gpu=index --format=csv,noheader)
gpu_index=($gpu_index_str)


for index in ${!gpu_index[*]}
do
    nvidia-settings -a "[gpu:$index]/GPUFanControlState=1"
done

old_speed=-1

# fan_nums  nvidia-settings -q all | grep fan
last_fan_index=$( nvidia-settings -q all | grep fan | grep -oP "\[fan:(\d+)\]:" | grep -oP "\d+" | sort -rn | head -n 1)

echo $last_fan_index
fan_nums=$((last_fan_index+1))
echo "fan count"
echo $fan_nums

while true
do
    temperature_str=$(nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader)
    temperature=($temperature_str)
    if [ $((temperature[0])) -lt 40 ]; then
        echo "temperature < 40"
        fan_speed=60
    elif [ $((temperature[0])) -lt 60 ]; then
        echo "40 < temperature < 60"
        fan_speed=90
    else
        echo "temperature > 60"
        fan_speed=100
    fi
    if [ $((old_speed)) -ne $((fan_speed)) ]; then
        for ((index=0;index<fan_nums;index++))
        do
          old_speed=$fan_speed
          # echo "fan speed flush to $fan_speed"
          
          nvidia-settings -a "[fan:$((index))]/GPUTargetFanSpeed=$((fan_speed))"
          # echo 'nvidia-settings -c:$index -a "[fan:$((index))]/GPUTargetFanSpeed=$((fan_speed))"'
        done
    fi
    sleep 300
done

缺陷:

  1. nvidia-settings 必须要在图形界面用, 所以不能用 ssh 远程调风扇速度, 相当于如果不设置开机自启的话, 每次重启服务器都要手动开个终端启动脚本, 而且需要连显示屏.
  2. 这里 sleep 300 是5分钟检查一次, 避免检查太频繁, 这里需要根据自己的情况调整.

已更新解决需要手动设置风扇数的问题.