There are mainly two approaches to do that:
- If you have to run a script, you don't convert it but rather call the script through a systemd service.
Therefore you need two files: the script and the "service" file. Let's say your script is called vgaoff
.
Place your script in /usr/lib/systemd/scripts
, make it executable. Then create a new service file in /usr/lib/systemd/system
(a plain text file, let's call it vgaoff.service
).
So this is the location of your files:
- the script:
/usr/lib/systemd/scripts/vgaoff
- the service:
/usr/lib/systemd/system/vgaoff.service
Now you have to edit the service file. Its content depends on how your script works:
If vgaoff
just powers off the gpu, e.g.:
exec blah-blah pwrOFF etc
then the content of vgaoff.service
should be:
[Unit]
Description=Power-off gpu
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/vgaoff
[Install]
WantedBy=multi-user.target
If vgaoff
is used to power off the GPU and also to power it back on, e.g.:
start() {
exec blah-blah pwrOFF etc
}
stop() {
exec blah-blah pwrON etc
}
case $1 in
start|stop) "$1" ;;
esac
then the content of vgaoff.service
should be:
[Unit]
Description=Power-off gpu
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/vgaoff start
ExecStop=/usr/lib/systemd/scripts/vgaoff stop
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Once you have the files in place and configured you can enable the service:
systemctl enable vgaoff.service
It should then start automatically after rebooting the machine.
- For the most trivial cases, you can do without the script and execute a certain command directly through the .service file:
To power off:
[Unit]
Description=Power-off gpu
[Service]
Type=oneshot
ExecStart=/bin/sh -c "echo OFF > /whatever/vga_pwr_gadget/switch"
[Install]
WantedBy=multi-user.target
To power off & on:
[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