HT3 / RK3568 飞牛 OS 智能风扇温控脚本部署指南
一、背景说明
夏天 HT3 刷飞牛后,默认的风扇控制策略可能不够理想。本文将详细介绍如何部署一个智能风扇温控脚本,实现根据 CPU 温度自动调节风扇转速。
适用场景:
- HT3 \ RK3568 系列开发板(如:ROC-RK3568-PC、IDO-EVB3568 等)
- 其他使用
pwm-fan驱动的 ARM 设备- 飞牛 OS(FNOS)系统
二、硬件检查
2.1 确认风扇控制接口
首先检查系统是否正确识别了风扇设备:
# 查看 hwmon 设备
ls -la /sys/class/hwmon/
# 查找 pwm-fan 设备
cat /sys/class/thermal/cooling_device*/type
正常情况下,应该看到输出为
pwm-fan。
2.2 查看风扇控制参数
# 查看风扇最大调速等级
cat /sys/class/thermal/cooling_device0/max_state
# 查看当前风扇等级
cat /sys/class/thermal/cooling_device0/cur_state
对于 RK3568 开发板,
max_state通常为8(0-8 共 9 个等级)。
2.3 手动测试风扇控制
在部署脚本前,建议先手动测试风扇是否正常工作:
# 测试停止
echo 0 > /sys/class/thermal/cooling_device0/cur_state
sleep 2
# 测试中速
echo 4 > /sys/class/thermal/cooling_device0/cur_state
sleep 2
# 测试全速
echo 8 > /sys/class/thermal/cooling_device0/cur_state
sleep 2
# 恢复停止状态
echo 0 > /sys/class/thermal/cooling_device0/cur_state
三、部署温控脚本
3.1 创建脚本文件
使用以下命令一键创建温控脚本(CPU 温度版和硬盘温度版二选一)。
选项 A:CPU 温度版
sudo bash -c 'cat > /usr/local/bin/fan_control.sh << "EOF"
#!/bin/bash
#===============================================
# 智能风扇温控脚本 (RK3568 多级调速版)
# 策略:
# < 45°C: 等级 0 (停止)
# 45-49°C: 等级 3 (低速)
# 50-54°C: 等级 5 (中速)
# 55-59°C: 等级 7 (高速)
# ≥ 60°C: 等级 8 (全速)
#===============================================
FAN_DEVICE="/sys/class/thermal/cooling_device0"
MAX_LEVEL=8
LOG_FILE="/var/log/fan_smart_control.log"
PID_FILE="/var/run/fan_control.pid"
trap "log \"收到退出信号,服务停止\"; exit 0" SIGTERM SIGINT
log() {
echo "$(date "+%Y-%m-%d %H:%M:%S"): $1" >> "$LOG_FILE"
}
set_fan_speed() {
local level=$1
if [ -z "$level" ] || [ "$level" -lt 0 ]; then
level=0
elif [ "$level" -gt "$MAX_LEVEL" ]; then
level=$MAX_LEVEL
fi
echo "$level" > "${FAN_DEVICE}/cur_state" 2>/dev/null
if [ $? -ne 0 ]; then
log "错误: 设置风扇等级 $level 失败"
return 1
fi
return 0
}
get_temperature() {
local temp_raw=$(cat /sys/class/thermal/thermal_zone0/temp 2>/dev/null)
if [ -z "$temp_raw" ]; then
return 1
fi
echo $((temp_raw / 1000))
}
if [ -f "$PID_FILE" ]; then
local old_pid=$(cat "$PID_FILE")
if kill -0 "$old_pid" 2>/dev/null; then
echo "错误: 脚本已在运行 (PID: $old_pid)"
exit 1
else
rm -f "$PID_FILE"
fi
fi
echo $$ > "$PID_FILE"
# 1. 禁用内核自动温控
echo disabled > /sys/class/thermal/thermal_zone0/mode 2>/dev/null
# 2. 停止并禁用冲突服务
systemctl stop pwm-fancontrol.service 2>/dev/null
systemctl disable pwm-fancontrol.service 2>/dev/null
systemctl mask pwm-fancontrol.service 2>/dev/null
log "========================================"
log "智能温控服务启动 (RK3568 最终版)"
log "已禁用内核自动温控和冲突服务"
log "策略: <45°C停, 45-49°C低速, 50-54°C中速, 55-59°C高速, ≥60°C全速"
log "========================================"
if [ ! -f "${FAN_DEVICE}/cur_state" ]; then
log "错误: 风扇设备不存在"
rm -f "$PID_FILE"
exit 1
fi
set_fan_speed 3
while true; do
temp=$(get_temperature)
if [ $? -ne 0 ] || [ -z "$temp" ]; then
set_fan_speed 3
log "温度读取失败,使用安全模式 (等级 3)"
sleep 3
continue
fi
if [ "$temp" -lt 45 ]; then
set_fan_speed 0
log "温度 ${temp}°C, 停止 (0)"
elif [ "$temp" -lt 50 ]; then
set_fan_speed 3
log "温度 ${temp}°C, 低速 (3)"
elif [ "$temp" -lt 55 ]; then
set_fan_speed 5
log "温度 ${temp}°C, 中速 (5)"
elif [ "$temp" -lt 60 ]; then
set_fan_speed 7
log "温度 ${temp}°C, 高速 (7)"
else
set_fan_speed $MAX_LEVEL
log "温度 ${temp}°C, 全速 (8)"
fi
sleep 3
done
rm -f "$PID_FILE"
EOF'
# 赋予执行权限
sudo chmod +x /usr/local/bin/fan_control.sh
选项 B:硬盘温度版
sudo tee /usr/local/bin/fan_control.sh > /dev/null << 'EOF'
#!/bin/bash
#===============================================
# 智能风扇温控脚本 (RK3568 硬盘温度版)
#===============================================
FAN_DEVICE="/sys/class/thermal/cooling_device0"
MAX_LEVEL=8
LOG_FILE="/var/log/fan_smart_control.log"
PID_FILE="/var/run/fan_control.pid"
trap "log \"收到退出信号,服务停止\"; exit 0" SIGTERM SIGINT
log() {
echo "$(date "+%Y-%m-%d %H:%M:%S"): $1" >> "$LOG_FILE"
}
set_fan_speed() {
local level=$1
if [ -z "$level" ] || [ "$level" -lt 0 ]; then
level=0
elif [ "$level" -gt "$MAX_LEVEL" ]; then
level=$MAX_LEVEL
fi
echo "$level" > "${FAN_DEVICE}/cur_state" 2>/dev/null
if [ $? -ne 0 ]; then
log "错误: 设置风扇等级 $level 失败"
return 1
fi
return 0
}
get_max_disk_temperature() {
local max_temp=0
local temp=0
if command -v hddtemp >/dev/null 2>&1; then
temp=$(hddtemp -n /dev/sd[a-z] 2>/dev/null | sort -rn | head -1)
if [ -n "$temp" ] && [ "$temp" -gt "$max_temp" ] 2>/dev/null; then
max_temp=$temp
fi
fi
if command -v smartctl >/dev/null 2>&1; then
for disk in /dev/sd[a-z] /dev/nvme[0-9]n[0-9]; do
if [ -e "$disk" ]; then
temp=$(smartctl -A "$disk" 2>/dev/null | grep -E "Temperature_Celsius|Temperature" | awk '{print $10}' | head -1)
if [ -n "$temp" ] && [ "$temp" -gt "$max_temp" ] 2>/dev/null; then
max_temp=$temp
fi
fi
done
fi
if [ "$max_temp" -eq 0 ] 2>/dev/null; then
return 1
fi
echo "$max_temp"
return 0
}
detect_method() {
if command -v hddtemp >/dev/null 2>&1; then
echo "hddtemp"
elif command -v smartctl >/dev/null 2>&1; then
echo "smartctl"
else
echo "none"
fi
}
if [ -f "$PID_FILE" ]; then
local old_pid=$(cat "$PID_FILE")
if kill -0 "$old_pid" 2>/dev/null; then
echo "错误: 脚本已在运行 (PID: $old_pid)"
exit 1
else
rm -f "$PID_FILE"
fi
fi
echo $$ > "$PID_FILE"
echo disabled > /sys/class/thermal/thermal_zone0/mode 2>/dev/null
systemctl stop pwm-fancontrol.service 2>/dev/null
systemctl disable pwm-fancontrol.service 2>/dev/null
systemctl mask pwm-fancontrol.service 2>/dev/null
METHOD=$(detect_method)
log "========================================"
log "智能温控服务启动 (RK3568 硬盘温度版)"
log "温度检测方法: $METHOD"
log "策略: <40°C停, 40-44°C低速, 45-49°C中速, 50-54°C高速, >=55°C全速"
log "========================================"
if [ ! -f "${FAN_DEVICE}/cur_state" ]; then
log "错误: 风扇设备不存在"
rm -f "$PID_FILE"
exit 1
fi
set_fan_speed 3
while true; do
temp=$(get_max_disk_temperature)
if [ $? -ne 0 ] || [ -z "$temp" ] || [ "$temp" -eq 0 ] 2>/dev/null; then
temp=$(cat /sys/class/thermal/thermal_zone0/temp 2>/dev/null | awk '{print int($1/1000)}')
if [ -z "$temp" ] || [ "$temp" -eq 0 ] 2>/dev/null; then
set_fan_speed 3
log "温度读取失败,使用安全模式 (等级 3)"
sleep 3
continue
fi
log "回退到CPU温度: ${temp}°C"
else
log "硬盘最高温度: ${temp}°C"
fi
if [ "$temp" -lt 40 ]; then
set_fan_speed 0
log "温度 ${temp}°C < 40°C, 停止 (0)"
elif [ "$temp" -lt 45 ]; then
set_fan_speed 3
log "温度 ${temp}°C (40-44°C), 低速 (3)"
elif [ "$temp" -lt 50 ]; then
set_fan_speed 5
log "温度 ${temp}°C (45-49°C), 中速 (5)"
elif [ "$temp" -lt 55 ]; then
set_fan_speed 7
log "温度 ${temp}°C (50-54°C), 高速 (7)"
else
set_fan_speed $MAX_LEVEL
log "温度 ${temp}°C >= 55°C, 全速 (8)"
fi
sleep 10
done
rm -f "$PID_FILE"
EOF
sudo chmod +x /usr/local/bin/fan_control.sh
3.2 验证脚本
# 检查脚本语法
bash -n /usr/local/bin/fan_control.sh
# 手动运行测试(按 Ctrl+C 停止)
sudo /usr/local/bin/fan_control.sh
四、配置系统服务
4.1 创建 systemd 服务文件
sudo bash -c 'cat > /etc/systemd/system/fan-control.service << "EOF"
[Unit]
Description=Smart Fan Control
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/fan_control.sh
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF'
4.2 启动并启用服务
# 重新加载 systemd 配置
sudo systemctl daemon-reload
# 启动服务
sudo systemctl start fan-control
# 设置开机自启
sudo systemctl enable fan-control
# 查看服务状态
sudo systemctl status fan-control
4.3 验证服务运行
# 查看实时日志
sudo tail -f /var/log/fan_smart_control.log
# 查看服务运行状态
sudo systemctl status fan-control
正常运行的日志示例:
2026-08-20 18:16:41: 温度 55°C (55-59°C), 高速 (等级 7) 2026-08-20 18:16:46: 温度 55°C (55-59°C), 高速 (等级 7) 2026-08-20 18:16:52: 温度 56°C (55-59°C), 高速 (等级 7)
五、常用管理命令
# 启动服务
sudo systemctl start fan-control
# 停止服务
sudo systemctl stop fan-control
# 重启服务
sudo systemctl restart fan-control
# 查看服务状态
sudo systemctl status fan-control
# 查看开机自启状态
sudo systemctl is-enabled fan-control
# 禁用开机自启
sudo systemctl disable fan-control
# 查看实时日志
sudo journalctl -u fan-control -f
# 查看温控日志
sudo tail -f /var/log/fan_smart_control.log
六、故障排查
6.1 服务启动失败
# 查看详细错误信息
sudo systemctl status fan-control -l --no-pager
sudo journalctl -u fan-control -n 50 --no-pager
# 检查脚本是否可执行
ls -la /usr/local/bin/fan_control.sh
# 手动运行测试
sudo /usr/local/bin/fan_control.sh
6.2 风扇不转
# 检查风扇设备是否存在
ls -la /sys/class/thermal/cooling_device0/
# 手动设置风扇等级测试
echo 8 > /sys/class/thermal/cooling_device0/cur_state
cat /sys/class/thermal/cooling_device0/cur_state
# 检查温度传感器
cat /sys/class/thermal/thermal_zone0/temp
6.3 脚本已运行但风扇无反应
# 查看当前风扇等级
cat /sys/class/thermal/cooling_device0/cur_state
# 查看温度
cat /sys/class/thermal/thermal_zone0/temp | awk '{print int($1/1000)}'
# 检查日志
tail -20 /var/log/fan_smart_control.log
七、卸载清理
如需卸载温控服务:
# 停止并禁用服务
sudo systemctl stop fan-control
sudo systemctl disable fan-control
# 删除服务文件
sudo rm /etc/systemd/system/fan-control.service
# 重新加载 systemd
sudo systemctl daemon-reload
# 删除脚本(可选)
sudo rm /usr/local/bin/fan_control.sh
# 清理日志(可选)
sudo rm /var/log/fan_smart_control.log
八、自定义调速策略
如需调整温控策略,可修改脚本中的温度阈值和对应等级:
# 修改温度阈值(单位:°C)
elif [ "$temp" -lt 45 ]; then # 45°C 以下停止
elif [ "$temp" -lt 50 ]; then # 45-49°C 低速
elif [ "$temp" -lt 55 ]; then # 50-54°C 中速
elif [ "$temp" -lt 60 ]; then # 55-59°C 高速
# 修改对应风扇等级(0-8)
set_fan_speed 0 # 停止
set_fan_speed 3 # 低速
set_fan_speed 5 # 中速
set_fan_speed 7 # 高速
set_fan_speed 8 # 全速
九、注意事项
- 硬件兼容性:本文方案适用于使用
pwm-fan驱动的 RK3568 开发板。 - 风扇类型:支持 4 针 PWM 调速风扇。
- 温度传感器:使用
thermal_zone0作为温度源。 - 权限管理:脚本需要 root 权限运行。
- 日志管理:日志文件位于
/var/log/fan_smart_control.log。
十、总结
通过本文的部署,你可以实现:
- ✅ 根据 CPU 温度自动调节风扇转速
- ✅ 开机自启动温控服务
- ✅ 完整的日志记录便于排查问题
- ✅ 可自定义温控策略
这个方案已在 HT3 上验证通过,可以有效平衡散热效果和噪音控制。

💬 评论区 0