systemd 脚本未应用内核配置设置(使 powertop 的建议永久生效)

systemd 脚本未应用内核配置设置(使 powertop 的建议永久生效)

问题陈述

我尝试通过在一次性 systemd 服务中执行的脚本中写入内核设置来使 PowerTop 提供的调整建议永久生效。脚本包含如下行

[...]

echo 'auto' > '/sys/bus/i2c/devices/i2c-6/device/power/control';

[...]

但出于某种原因,启动后设置未应用。systemctl start powertop登录后在终端中手动运行可以正确应用参数。我不明白为什么它在登录后有效,但在启动时无效。

该方法的灵感来自于如何使 Powertop 更改永久生效?

配置

服务的定义/etc/systemd/system/powertop.service如下

[Unit]
Description=Power tuning based on PowerTOP

[Service]
Type=oneshot
Environment="TERM=dumb"
RemainAfterExit=true
ExecStart=/home/XXXX/Documents/scripts/powertop/powertune.sh

[Install]
WantedBy=multi-user.target

写入内核配置的脚本如下

#!/bin/bash
echo "power tuning"

whoami

echo 'auto' > '/sys/bus/i2c/devices/i2c-6/device/power/control';
echo 'auto' > '/sys/bus/i2c/devices/i2c-7/device/power/control';
echo 'auto' > '/sys/bus/i2c/devices/i2c-0/device/power/control';
echo 'auto' > '/sys/bus/i2c/devices/i2c-1/device/power/control';
echo 'auto' > '/sys/bus/i2c/devices/i2c-2/device/power/control';

echo 'auto' > '/sys/bus/i2c/devices/i2c-3/device/power/control';

echo 'auto' > '/sys/bus/pci/devices/0000:00:02.0/power/control';
echo 'auto' > '/sys/block/sda/device/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:14.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.4/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.2/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:17.0/ata2/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:17.0/ata3/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:1c.5/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:17.0/ata1/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:17.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:14.2/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:16.3/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:00.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.3/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:01:00.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:04.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:02:00.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:1c.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.6/power/control';

echo 'disabled' > '/sys/bus/usb/devices/1-5.1/power/wakeup';

验证已完成

该服务已通过 正确启用systemctl enable powertop,并且我可以使用 验证systemctl status powertop它确实是在启动时执行的。

root@cs0:/etc/systemd/system# systemctl status powertop
● powertop.service - Power tuning based on PowerTOP
     Loaded: loaded (/etc/systemd/system/powertop.service; enabled; vendor preset: enabled)
     Active: active (exited) since Mon 2022-11-14 19:36:00 CET; 9s ago
    Process: 6584 ExecStart=/home/XXXXX/Documents/scripts/powertop/powertune.sh (code=exited, status=0/SUCCESS)
   Main PID: 6584 (code=exited, status=0/SUCCESS)
        CPU: 6ms

Nov 14 19:36:00 cs0 systemd[1]: Starting Power tuning based on PowerTOP...
Nov 14 19:36:00 cs0 powertune.sh[6584]: power tuning
Nov 14 19:36:00 cs0 powertune.sh[6585]: root
Nov 14 19:36:00 cs0 systemd[1]: Finished Power tuning based on PowerTOP.

系统

  • Ubuntu 22.04.1 LTS
  • Linux cs0 5.15.0-52-generic #58-Ubuntu SMP 星期四 十月 13 08:03:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

任何帮助都将非常感激。

相关内容