曂沅仴駦 发表于 2024-10-28 08:45:37

Linux 服务器 gpu 风扇控制,无桌面

linux 服务器因为没有安装桌面,gpu 风扇转速控制一直失败,搞了几天终于成功了,记载一下。
1、利用 nvidia-settings 需要开启一个 X server 才能利用

#开启一个X server
X :1 &\

# 要关闭的话,用ps查找id, 手动kill

ps -fC Xorg
sudo kill pid
2、 然后更改 GPU 状态和风扇转速

nvidia-settings --display :1.0 -a "/GPUFanControlState=1" -a "/GPUTargetFanSpeed=60"
#或者
nvidia-settings --display :1.0 -a "/GPUFanControlState=1" -a "/GPUTargetFanSpeed=60"

修改之前
https://i-blog.csdnimg.cn/blog_migrate/c67b5dabdba8f801e97fafd9c7a4de56.png
修改后
https://i-blog.csdnimg.cn/blog_migrate/027c9934dc09bbf4ac1a8bd59d96e9a8.png
3、写一个主动控制的脚本时刻监测温度,并修改风扇转速

sudo mkdir /opt/cool_gpus/
sudo vim /opt/cool_gpus/cool_gpus.sh
#!/bin/bash
cd $(dirname $0)
X :1 &\
# 检查是否有无限运行的参数
run_forever=1


# 设置日期格式
date_format="+%Y-%m-%d %H:%M:%S"

# 定义日志文件,文件名包含当前日期
log_file="gpu_fan_log_$(date "+%Y%m%d").txt"

# 循环直到脚本被告知停止
while : ; do
    # 获取所有NVIDIA GPU的数量
    gpu_count=$(nvidia-smi -L | wc -l)

    # 为每个GPU设置风扇速度
    for (( gpuid=0; gpuid<gpu_count; gpuid++ )); do
      # 获取当前GPU的温度
      temp=$(nvidia-smi --query-gpu=temperature.gpu --format=csv,noheader,nounits -i ${gpuid})

      # 根据温度确定目标风扇速度
      if [ "$temp" -lt 30 ]; then
            target=20
      elif [ "$temp" -lt 40 ]; then
            target=30
      elif [ "$temp" -lt 50 ]; then
            target=40
      elif [ "$temp" -lt 60 ]; then
            target=50
      elif [ "$temp" -lt 70 ]; then
            target=60
      elif [ "$temp" -lt 75 ]; then
            target=70
      elif [ "$temp" -lt 80 ]; then
            target=80
      elif [ "$temp" -lt 85 ]; then
            target=95
      else
            target=99
      fi

      # 输出当前温度和目标风扇速度,并将输出重定向到日志文件
      echo "$(date "$date_format") - GPU ${gpuid}: Current temperature is ${temp} C. Setting fan speed to ${target}%." >> $log_file

      # 设置当前GPU的风扇速度
      nvidia-settings --display :1.0 -a "/GPUFanControlState=1" -a "/GPUTargetFanSpeed=${target}"
    done
    # 检查是否设置为无限运行
    if [ "${run_forever}" -ne 1 ]; then
      break
    fi

    # 等待一定时间再次检查
    sleep 15
done
4、 添加权限

sudo chmod +x /opt/cool_gpus/cool_gpus.sh
5、设置开机自启

起首,创建一个新的systemd服务文件。这个文件通常位于/etc/systemd/system目次下,文件名以.service末端。我们创建一个名为gpu_fan_control.service的文件:
sudo vim /etc/systemd/system/gpu_fan_control.service
输入下面的内容

Description=GPU Fan Control Script


ExecStart=/opt/cool_gpus/cool_gpus.sh


WantedBy=multi-user.target
生存后关闭文件,加在配置文件
sudo systemctl daemon-reload
开机启动
sudo systemctl enable gpu_fan_control
立即启动
sudo systemctl start gpu_fan_control
检查状态
sudo systemctl status gpu_fan_control

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Linux 服务器 gpu 风扇控制,无桌面