十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

thinkfan部署指南:systemd开机自启全解,详解休眠唤醒2个隐藏信号(SIGPWR与SIGUSR2)

thinkfan部署指南:systemd开机自启全解,详解休眠唤醒2个隐藏信号(SIGPWR与SIGUSR2) thinkfan部署指南systemd开机自启全解详解休眠唤醒2个隐藏信号SIGPWR与SIGUSR2【免费下载链接】thinkfanThe minimalist fan control program项目地址: https://gitcode.com/gh_mirrors/th/thinkfanthinkfan是一款极简、轻量的 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_NVMLON读取 NVIDIA GPU 温度、-DUSE_LM_SENSORSON读取 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.serviceforking常驻主服务开机自启管理 PID 文件/run/thinkfan.pidthinkfan-sleep.serviceoneshot休眠前给 thinkfan 发送SIGPWRthinkfan-wakeup.serviceoneshot唤醒后给 thinkfan 发送SIGUSR2以主服务 thinkfan.service.cmake 为例关键配置如下[Service] Typeforking ExecStart/usr/local/sbin/thinkfan $THINKFAN_ARGS PIDFile/run/thinkfan.pid ExecReload/bin/kill -HUP $MAINPID注意Alsothinkfan-sleep.service和Alsothinkfan-wakeup.service两行——执行systemctl enable thinkfan时会自动把休眠/唤醒两个伴生服务一起启用这是隐藏信号能自动工作的关键手动写服务文件的用户很容易漏掉这一步。四、详解 2 个隐藏信号SIGPWR 与 SIGUSR2 SIGPWR —— 我要睡了传感器可能掉线背景系统进入 suspend/hibernate 后很多温度传感器需要几秒才能重新就绪。如果 thinkfan 醒来后立刻读传感器会因读取失败而报错。机制thinkfan-sleep.service在sleep.target之前执行pkill -x -pwr thinkfanthinkfan 收到 SIGPWR 后的处理逻辑case SIGPWR: tolerate_errors 4; // 允许接下来 4 个循环出现传感器读取错误即容忍唤醒后前 4 个循环的传感器读取错误给硬件缓冲时间避免误报。服务文件里还有一处小 hacksleep 1延时 1 秒防止信号处理与休眠流程发生竞态源码注释明确写着 this is a race workaround。⚡ SIGUSR2 —— 醒了重新接管风扇背景多数风扇驱动如hwmon、thinkpad_acpi在系统唤醒后会自动把风扇重置为固件自动控制模式。若不重新初始化thinkfan 的调速会失效风扇又回到原来的狂转状态。机制thinkfan-wakeup.service在suspend.target、hibernate.target等目标之后执行pkill -x -usr2 thinkfanthinkfan 收到 SIGUSR2 后会重新执行风扇初始化case SIGUSR2: interrupted signum; // 退出主循环 ... config-init_fans(); // 重新初始化风扇控制 这两个信号就是非 systemd 发行版如 OpenRC下需要自己用 systemd-notify 或脚本手动模拟的原因——man 页 thinkfan.1.cmake 明确提示non-systemd 环境需使用其他机制发送这两个信号。附赠thinkfan 完整信号速查表信号发送命令效果SIGHUPpkill -x -HUP thinkfan热重载配置文件新配置出错则保留旧配置SIGUSR1pkill -x -USR1 thinkfan把当前所有温度 dump 到 syslog/终端SIGPWRpkill -x -PWR thinkfan预告休眠容忍 4 个循环的传感器错误SIGUSR2pkill -x -USR2 thinkfan重新初始化风扇控制唤醒后必发SIGINT/SIGTERMpkill -x -TERM thinkfan干净退出五、实战调优与常见问题调风扇响应灵敏度bias风扇转速变化太跳就调低偏置变化太慢就调高偏置。官方推荐用 systemd override 覆盖参数模板见 override.confsudo systemctl edit thinkfan # 在打开的编辑器中写入 # [Service] # EnvironmentTHINKFAN_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休眠信号SIGPWRthinkfan-sleep.service 自动发送唤醒信号SIGUSR2thinkfan-wakeup.service 自动发送调偏置systemctl edit thinkfan设置-b参数thinkfan 用极小的体量解决了风扇噪音 休眠唤醒失控两大痛点systemd 的三个服务文件 两个隐藏信号构成了完整的生命周期管理。掌握 SIGPWR 与 SIGUSR2你的笔记本风扇管理才算真正闭环。【免费下载链接】thinkfanThe minimalist fan control program项目地址: https://gitcode.com/gh_mirrors/th/thinkfan创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表