You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

休眠恢复后自动执行的脚本需求:维持CPU最低频率设置

解决休眠恢复后自动设置CPU为最低频率的方案

我明白你的需求——每次从磁盘休眠恢复后,系统总会把CPU切回默认变频模式,导致风扇时不时启动,你希望恢复后自动锁定CPU到最低频率实现完全静音,需要高性能时再手动切换。下面是一套可靠的实现方案:

1. 编写CPU低频率设置脚本

先创建一个可执行脚本,用来将CPU切换到最低功耗模式(以powersave调速器为例,它会让CPU尽可能维持在最低频率,完美实现静音)。

  • 创建脚本文件,比如/usr/local/bin/set-cpu-lowfreq.sh,内容如下:
#!/bin/bash

# 方案1:用cpupower工具统一设置所有核心(需先安装cpupower,多数发行版自带)
cpupower frequency-set --governor powersave

# 方案2:直接操作sysfs文件(兼容所有Linux发行版,无需额外工具)
# for core in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
#     echo powersave > "$core"
# done

# 进阶可选:直接锁定CPU到硬件最低频率(如果powersave模式仍有小幅提频)
# MIN_FREQ=$(cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq)
# for core in /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq; do
#     echo $MIN_FREQ > "$core"
# done
  • 给脚本添加执行权限:
sudo chmod +x /usr/local/bin/set-cpu-lowfreq.sh

你可以先手动运行sudo /usr/local/bin/set-cpu-lowfreq.sh测试,通过cpupower frequency-info查看CPU调速器状态,或者直接观察风扇是否停止运行。

2. 创建休眠恢复自动触发的systemd服务

现在需要让这个脚本在系统从休眠/磁盘休眠恢复时自动运行,用systemd的恢复钩子就能实现:

  • 创建服务文件/etc/systemd/system/cpu-lowfreq-resume.service,内容如下:
[Unit]
Description=Auto set CPU to lowest frequency on resume from sleep
After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/set-cpu-lowfreq.sh
User=root

[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target
  • 重新加载systemd并启用服务:
sudo systemctl daemon-reload
sudo systemctl enable cpu-lowfreq-resume.service

3. 高性能模式切换脚本(按需准备)

如果你需要临时切换到高性能模式,再写一个简单的切换脚本即可:

#!/bin/bash
# 保存为/usr/local/bin/set-cpu-highperf.sh

cpupower frequency-set --governor performance
# 或者用sysfs方式:
# for core in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do
#     echo performance > "$core"
# done

同样给它添加执行权限,需要高性能时手动运行sudo /usr/local/bin/set-cpu-highperf.sh就行。

验证方案

现在可以测试一下:让笔记本进入磁盘休眠,恢复后运行cpupower frequency-info,确认CPU调速器已经自动切换为powersave,或者直接观察风扇是否保持静音状态。

注意:部分品牌笔记本(如ThinkPad、戴尔)有自己的厂商电源管理工具,可能会覆盖系统的CPU频率设置。如果上述方案不生效,可以先禁用厂商的自动电源管理功能再尝试。

内容的提问来源于stack exchange,提问作者Mephisto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:53:14