Sha*_*que 257 init-script shell-script systemd
我的笔记本电脑上有 2 个显卡。一个是IGP,另一个是离散的。
我写了一个shell脚本来关闭独立显卡。
如何将其转换为 systemd 脚本以在启动时运行它?
don*_*sti 373
主要有两种方法可以做到这一点:
systemd
服务运行脚本。因此您需要两个文件:脚本和.service
文件(单元配置文件)。
确保您的脚本是可执行的,并且第一行(shebang)是#!/bin/sh
. 然后.service
在/etc/systemd/system
(一个纯文本文件,我们称之为vgaoff.service
)中创建文件。
例如:
/usr/bin/vgaoff
/etc/systemd/system/vgaoff.service
现在,编辑单元文件。它的内容取决于您的脚本如何工作:
如果vgaoff
只是关闭 GPU,例如:
exec blah-blah pwrOFF etc
Run Code Online (Sandbox Code Playgroud)
那么内容vgaoff.service
应该是:
[Unit]
Description=Power-off gpu
[Service]
Type=oneshot
ExecStart=/usr/bin/vgaoff
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
如果vgaoff
用于关闭 GPU 并重新打开它,例如:
start() {
exec blah-blah pwrOFF etc
}
stop() {
exec blah-blah pwrON etc
}
case $1 in
start|stop) "$1" ;;
esac
Run Code Online (Sandbox Code Playgroud)
那么内容vgaoff.service
应该是:
[Unit]
Description=Power-off gpu
[Service]
Type=oneshot
ExecStart=/usr/bin/vgaoff start
ExecStop=/usr/bin/vgaoff stop
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
关闭电源:
[Unit]
Description=Power-off gpu
[Service]
Type=oneshot
ExecStart=/bin/sh -c "echo OFF > /whatever/vga_pwr_gadget/switch"
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
要关闭和打开电源:
[Unit]
Description=Power-off gpu
[Service]
Type=oneshot
ExecStart=/bin/sh -c "echo OFF > /whatever/vga_pwr_gadget/switch"
ExecStop=/bin/sh -c "echo ON > /whatever/vga_pwr_gadget/switch"
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
完成文件后,启用该服务:
systemctl enable vgaoff.service
Run Code Online (Sandbox Code Playgroud)
它会在下次启动时自动启动。您甚至可以一次性启用和启动服务
systemctl enable --now vgaoff.service
Run Code Online (Sandbox Code Playgroud)
截至systemd v.220
(在较旧的设置中,您必须手动启动它)。
有关更多详细信息,请参阅systemd.service
手册页。
故障排除。
从这里开始:
如何查看systemd
服务的完整日志?
systemd
服务退出代码和状态信息说明
归档时间: |
|
查看次数: |
488929 次 |
最近记录: |