怎么用脚本控制风扇转速

wen 实用脚本 1

我来介绍几种控制风扇转速的脚本方法,按不同操作系统分别说明:

怎么用脚本控制风扇转速

Linux 系统

使用 lm-sensors 和 fancontrol

# 安装工具
sudo apt-get install lm-sensors fancontrol
# 检测传感器
sudo sensors-detect
# 生成配置文件
sudo pwmconfig

手动控制脚本示例

#!/bin/bash
# fan_control.sh
# 设置风扇为手动模式
echo 1 > /sys/class/hwmon/hwmon0/pwm1_enable
# 设置转速(0-255)
echo 180 > /sys/class/hwmon/hwmon0/pwm1
# 根据温度自动调整
while true; do
    temp=$(sensors | grep 'Core 0' | awk '{print $3}' | cut -d'+' -f2 | cut -d'.' -f1)
    if [ $temp -gt 70 ]; then
        echo 200 > /sys/class/hwmon/hwmon0/pwm1
    elif [ $temp -gt 50 ]; then
        echo 150 > /sys/class/hwmon/hwmon0/pwm1
    else
        echo 100 > /sys/class/hwmon/hwmon0/pwm1
    fi
    sleep 5
done

使用 nbfc (NoteBook FanControl)

# 安装
git clone https://github.com/hirschmann/nbfc.git
cd nbfc
./install.sh
# 控制命令
nbfc start -a
nbfc set -s 50

Windows 系统

PowerShell 脚本(需管理员权限)

# fan_control.ps1
# 使用 WMI 控制风扇
$fanSpeed = 50  # 设置转速百分比
# 获取风扇控制对象
$fan = Get-WmiObject -Namespace "root\wmi" -Class MSAcpi_ThermalZoneTemperature
# 使用 SpeedFan 或厂商工具 API
# 这里以 SpeedFan 为例
$speedfan = New-Object -ComObject SpeedFan.Control
$speedfan.SetSpeed(0, $fanSpeed)

批处理文件

@echo off
:: fan_control.bat
:: 调用厂商工具控制风扇
:: 对于 Dell 笔记本
dell-biosutil.exe --set-fan-speed 50%
:: 对于 ThinkPad
acpicontrol.exe fan level 3

第三方工具脚本

SpeedFan (Windows)

# 使用命令行参数
speedfan.exe -a 50@0

NVIDIA 显卡风扇控制

# Linux
nvidia-settings -a "[gpu:0]/GPUFanControlState=1"
nvidia-settings -a "[fan:0]/GPUCurrentFanSpeed=50"
# Windows
nvidia-smi -pl 80

完整的智能温控脚本示例

#!/usr/bin/env python3
# smart_fan_control.py
import os
import time
import subprocess
class FanController:
    def __init__(self):
        self.hwmon_path = "/sys/class/hwmon/hwmon0"
        self.pwm_path = f"{self.hwmon_path}/pwm1"
        self.temp_path = f"{self.hwmon_path}/temp1_input"
    def read_temp(self):
        # 读取温度
        with open(self.temp_path, 'r') as f:
            temp = int(f.read()) / 1000
        return temp
    def set_speed(self, speed):
        # 设置风扇转速
        with open(self.pwm_path, 'w') as f:
            f.write(str(speed))
    def auto_control(self):
        # 自动温控逻辑
        while True:
            temp = self.read_temp()
            if temp > 70:
                self.set_speed(255)  # 全速
            elif temp > 60:
                self.set_speed(200)
            elif temp > 50:
                self.set_speed(150)
            else:
                self.set_speed(100)  # 低速
            time.sleep(5)
if __name__ == "__main__":
    controller = FanController()
    controller.auto_control()

使用方法

  1. 查看设备信息
    # Linux
    ls /sys/class/hwmon/
    cat /sys/class/hwmon/hwmon0/name

查看当前风扇状态

watch -n 1 "sensors && cat /sys/class/hwmon/hwmon0/fan"


2. **运行脚本**
```bash
# 添加执行权限
chmod +x fan_control.sh
# 后台运行
sudo ./fan_control.sh &
  1. 开机自启(可选)
    # 添加到 rc.local 或 systemd
    sudo crontab -e
    # 添加: @reboot /path/to/fan_control.sh

注意事项

⚠️ 安全提示

  • 控制风扇前确保了解硬件规格
  • 保留适当的温度余量
  • 监控系统温度以防过热
  • 不同硬件可能使用不同的控制接口
  • 某些系统可能需要ACPI或BIOS支持

你使用的是哪种操作系统和硬件设备?我可以提供更针对性的配置方案。

抱歉,评论功能暂时关闭!