使用 systemctl 永久更改 powertop

使用 systemctl 永久更改 powertop

我有一个 .sh 脚本,试图使 powertop 更改永久生效。

'auto' > '/sys/bus/pci/devices/0000:00:02.1/power/control';

此.sh脚本中的命令均属于此类型。

我在 /etc/systemd/system 中有一个 systemd-service,其内容如下:

[Unit]
Description=My Script
After=network.target

[Service]
Type=forking
ExecStart=/home/my_username/.autostart_sudo.sh

[Install]
WantedBy=multi-user.target

启动后,脚本“ .autostart_sudo.sh”被正确调用,(例如,此文件中的“蓝牙关闭”命令被完美调用)但 echo-> 命令未被成功调用。

我注意到打字

  sudo 'auto' > '/sys/bus/pci/devices/0000:00:02.1/power/control';

在终端上出现限制错误,但是

  sudo su
 'auto' > '/sys/bus/pci/devices/0000:00:02.1/power/control';

作品。

我该如何改变我的 systemd+bash 脚本才能使 powertop-changes 在启动时正常工作?

提前致谢!

编辑:

好的,我再次尝试描述流程:我在本帖顶部发布了 systemd 服务。此服务调用我的 /home/user/ 中的“.autostart_sudo.sh”。

这个 .autostart_sudo.sh 中有很多命令,如下所示:

echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.2/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:04:00.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:03:00.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:1a.0/power/control';
echo 'auto' > '/sys/bus/pci/devices/0000:00:03.3/power/control';
(...)
thinkfan start

“thinkfan start” 也需要 root 权限才能正确启动,这个可以运行,但是“echo”命令则不行。

我也试过

bash -c 'echo auto > /sys/bus/pci/devices/0000:00:1d.0/power/control;'
bash -c 'echo auto > /sys/bus/pci/devices/0000:00:1a.7/power/control;'
bash -c 'echo auto > /sys/bus/pci/devices/0000:00:1a.2/power/control;'
bash -c 'echo auto > /sys/bus/pci/devices/0000:00:02.0/power/control;'
bash -c 'echo auto > /sys/bus/pci/devices/0000:00:02.1/power/control;'  

也不起作用 :/

我上面提到的 sudo-test-stuff 只是测试在终端

有什么建议吗?

答案1

您确定为 /home/my_username/.autostart_sudo.sh 设置了 X-bit 吗?请记住,此脚本的所有者 uid/gid 必须是 root,以防止恶意软件注入 shell 代码。并且

sudo echo 'auto' > '/sys/bus/pci/devices/0000:00:02.1/power/control';

不起作用,因为“>”不是由 root 执行的。请尝试

sudo bash -c 'echo auto > /sys/bus/pci/devices/0000:00:02.1/power/control';

答案2

是的,我的错。我tlp start在 Powertop 调整后打电话。我不知道 tlp 会把设置搞得这么糟。所以,如果你有同样的问题:请致电tlp start

答案3

太伤心了,最后我不得不介入并回答这个问题......

之前我无法回答,因为我没有回答问题、评论、点赞的声誉。所以,如果你想要更多答案,那就点赞这个答案吧

你必须做两件事:

1)自动为每个需要 sudo 权限的命令提供密码。

echo "PASSWORD" | sudo -S COMMAND

2)授予每个人更改每个配置文件内容的权限

chmod 666 File_Path

这是对我有用的代码:

使用您当前的帐户 sudo 密码更改“PASSWORD”

#For File Permission Change

echo "PASSWORD" | sudo -S chmod 666 '/sys/class/scsi_host/host0/link_power_management_policy'
echo "PASSWORD" | sudo -S chmod 666 '/sys/class/scsi_host/host1/link_power_management_policy'
echo "PASSWORD" | sudo -S chmod 666 '/sys/module/snd_hda_intel/parameters/power_save'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/i2c/devices/i2c-5/device/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/usb/devices/3-1.6/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:1b.0/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:16.0/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:1f.0/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:1c.0/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:1f.2/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:1c.2/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:14.0/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:1d.0/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:06:00.0/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:03.0/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:00.0/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:1c.3/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:07:00.0/power/control'
echo "PASSWORD" | sudo -S chmod 666 '/sys/bus/pci/devices/0000:00:1c.4/power/control'

#For Actual Modification

echo 'min_power' > '/sys/class/scsi_host/host0/link_power_management_policy'
echo 'min_power' > '/sys/class/scsi_host/host1/link_power_management_policy'
echo '1' > '/sys/module/snd_hda_intel/parameters/power_save'
echo 'auto' > '/sys/bus/i2c/devices/i2c-5/device/power/control'
echo 'auto' > '/sys/bus/usb/devices/3-1.6/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1b.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:16.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1c.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1f.2/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1c.2/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:14.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1d.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:06:00.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:03.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:00.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1c.3/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:07:00.0/power/control'
echo 'auto' > '/sys/bus/pci/devices/0000:00:1c.4/power

注意:你可能必须根据你的系统更改整个命令:)

相关内容