thinkfan部署指南:systemd开机自启全解,详解休眠唤醒2个隐藏信号(SIGPWR与SIGUSR2)
【免费下载链接】thinkfanThe minimalist fan control program项目地址: https://gitcode.com/gh_mirrors/th/thinkfan
🌀thinkfan是一款极简、轻量的 Linux 风扇控制程序(fan control),它根据 CPU、硬盘等传感器温度按预设曲线调节风扇转速。本文将带你完成 thinkfan 的编译部署与 systemd 开机自启配置,并深入讲解让它在休眠/唤醒后正常工作的 2 个隐藏信号:SIGPWR 与 SIGUSR2。
一、thinkfan 是什么?为什么需要它?
笔记本(尤其是 ThinkPad)在重度负载下风扇常常"狂转",而轻负载时又可能散热不足。thinkfan 把风扇的控制权从系统固件手里"接管"过来,实现:
- 📉安静模式:按温度-转速曲线精细控速,低负载时风扇几乎无声
- 🛡️保护模式:高温时自动拉满转速,防止过热
- ⚙️纯用户态运行:不依赖专有驱动,通过内核
hwmon接口读写
⚠️ 官方提醒:thinkfan 只做了基础配置校验,温度限值设置得过于极端可能缩短硬件寿命。部署前请认真理解配置文件。
二、thinkfan 快速部署:从源码到开机自启
1️⃣ 安装编译依赖
Debian/Ubuntu 系:
sudo apt install -y cmake-curses-gui build-essential cmake g++ \ libyaml-cpp-dev pkgconfig libsensors-devFedora/EL 系:
sudo dnf install -y cmake g++ pkgconfig yaml-cpp-devel lm_sensors-devel2️⃣ 获取源码并编译
git clone https://gitcode.com/gh_mirrors/th/thinkfan cd thinkfan && mkdir build && cd build cmake .. make sudo make install默认安装到/usr/local/sbin/thinkfan,可选构建参数见 CMakeLists.txt,例如-DUSE_NVML=ON(读取 NVIDIA GPU 温度)、-DUSE_LM_SENSORS=ON(读取 lm-sensors 温度,默认开启)。
3️⃣ 一键启用 systemd 开机自启
CMake 在安装时会自动检测系统:检测到systemd就安装 systemd 服务文件,检测到OpenRC就安装 initscript。systemd 系统下只需一条命令:
sudo systemctl enable --now thinkfan🎉 部署完成。验证服务状态:
systemctl status thinkfan journalctl -u thinkfan -f # 实时查看风扇控制日志三、systemd 服务文件拆解:3 个文件各司其职
安装时会在/usr/lib/systemd/system/下生成 3 个服务单元,源码位于 rcscripts/systemd/:
| 服务文件 | 类型 | 作用 |
|---|---|---|
thinkfan.service | forking(常驻) | 主服务,开机自启,管理 PID 文件/run/thinkfan.pid |
thinkfan-sleep.service | oneshot | 休眠前给 thinkfan 发送SIGPWR |
thinkfan-wakeup.service | oneshot | 唤醒后给 thinkfan 发送SIGUSR2 |
以主服务 thinkfan.service.cmake 为例,关键配置如下:
[Service] Type=forking ExecStart=/usr/local/sbin/thinkfan $THINKFAN_ARGS PIDFile=/run/thinkfan.pid ExecReload=/bin/kill -HUP $MAINPID注意Also=thinkfan-sleep.service和Also=thinkfan-wakeup.service两行——执行systemctl enable thinkfan时会自动把休眠/唤醒两个伴生服务一起启用,这是"隐藏信号"能自动工作的关键,手动写服务文件的用户很容易漏掉这一步。
四、详解 2 个隐藏信号:SIGPWR 与 SIGUSR2
💤 SIGPWR —— "我要睡了,传感器可能掉线"
背景:系统进入 suspend/hibernate 后,很多温度传感器需要几秒才能重新就绪。如果 thinkfan 醒来后立刻读传感器,会因读取失败而报错。
机制:thinkfan-sleep.service在sleep.target之前执行pkill -x -pwr thinkfan,thinkfan 收到 SIGPWR 后的处理逻辑:
case SIGPWR: tolerate_errors = 4; // 允许接下来 4 个循环出现传感器读取错误即:容忍唤醒后前 4 个循环的传感器读取错误,给硬件"缓冲时间",避免误报。服务文件里还有一处小 hack:sleep 1延时 1 秒,防止信号处理与休眠流程发生竞态(源码注释明确写着 this is a race workaround)。
⚡ SIGUSR2 —— "醒了!重新接管风扇"
背景:多数风扇驱动(如hwmon、thinkpad_acpi)在系统唤醒后会自动把风扇重置为固件自动控制模式。若不重新初始化,thinkfan 的调速会"失效",风扇又回到原来的狂转状态。
机制:thinkfan-wakeup.service在suspend.target、hibernate.target等目标之后执行pkill -x -usr2 thinkfan,thinkfan 收到 SIGUSR2 后会重新执行风扇初始化:
case SIGUSR2: interrupted = signum; // 退出主循环 ... config->init_fans(); // 重新初始化风扇控制💡 这两个信号就是非 systemd 发行版(如 OpenRC)下需要自己用 systemd-notify 或脚本手动模拟的原因——man 页 thinkfan.1.cmake 明确提示:non-systemd 环境需使用其他机制发送这两个信号。
附赠:thinkfan 完整信号速查表
| 信号 | 发送命令 | 效果 |
|---|---|---|
SIGHUP | pkill -x -HUP thinkfan | 热重载配置文件(新配置出错则保留旧配置) |
SIGUSR1 | pkill -x -USR1 thinkfan | 把当前所有温度 dump 到 syslog/终端 |
SIGPWR | pkill -x -PWR thinkfan | 预告休眠:容忍 4 个循环的传感器错误 |
SIGUSR2 | pkill -x -USR2 thinkfan | 重新初始化风扇控制(唤醒后必发) |
SIGINT/SIGTERM | pkill -x -TERM thinkfan | 干净退出 |
五、实战调优与常见问题
调风扇响应灵敏度(bias)
风扇转速变化太"跳"就调低偏置,变化太慢就调高偏置。官方推荐用 systemd override 覆盖参数(模板见 override.conf):
sudo systemctl edit thinkfan # 在打开的编辑器中写入: # [Service] # Environment='THINKFAN_ARGS=-b0'-b取值范围-10 ~ +30,对应 thinkfan.cpp 中的bias_level = b / 10。修改后systemctl restart thinkfan生效。
常见问题排查
- 唤醒后风扇不听 thinkfan 调?检查
thinkfan-wakeup.service是否已启用:systemctl is-enabled thinkfan-wakeup。 - 日志出现 "Userspace fan control had to be automatically re-initialized"?说明驱动被重置过,thinkfan 已自动恢复;fans.cpp 会提示应确保 wakeup 服务在位。
- 改了配置不生效?发 SIGHUP 热重载即可,无需重启服务。
总结
| 步骤 | 命令 |
|---|---|
| 编译安装 | cmake .. && make && sudo make install |
| 开机自启 | sudo systemctl enable --now thinkfan |
| 休眠信号 | SIGPWR(thinkfan-sleep.service 自动发送) |
| 唤醒信号 | SIGUSR2(thinkfan-wakeup.service 自动发送) |
| 调偏置 | systemctl edit thinkfan设置-b参数 |
thinkfan 用极小的体量解决了"风扇噪音 + 休眠唤醒失控"两大痛点,systemd 的三个服务文件 + 两个隐藏信号构成了完整的生命周期管理。掌握 SIGPWR 与 SIGUSR2,你的笔记本风扇管理才算真正闭环。🚀
【免费下载链接】thinkfanThe minimalist fan control program项目地址: https://gitcode.com/gh_mirrors/th/thinkfan
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考