воскресенье, 13 марта 2016 г.

Systemd custom script start/stop

You need two files: the script and the .service file (unit configuration file).
Make sure your script is executable and the first line (the shebang) is #!/bin/sh. Then create the .service file in /etc/systemd/system (a plain text file, let's call it vgaoff.service).
For example:
  1. the script: /usr/bin/vgaoff
  2. the unit file: /etc/systemd/system/vgaoff.service
Now, edit the unit 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/bin/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/bin/vgaoff start
ExecStop=/usr/bin/vgaoff stop
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Once you're done with the files, enable the service:


systemctl enable vgaoff.service
It should start automatically after rebooting the machine.

http://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd
http://www.freedesktop.org/software/systemd/man/systemd.service.html