Service on startup

Hi there

I want to run a script on startup, preferably before Volumio emits the sounds indicating it has booted up. What is the best way to do this?

  1. Make the script a service that runs on boot (not sure this will run before sound)
  2. Write an addon/plugin that somehow hooks into the boot sequence before the sound is played

Thanks!

I believe a plugin will always run before the startup sound, but… I’m kind of interested, because I have a plugin that preferably run after the startup sound.

You could create and run a systemd service script.
Something like this (I took the idea from an example by Michelangelo; thanks):

create a file

/lib/systemd/system/yourscript.service

and another one

/usr/bin/yourscript.sh

fill it with something like this

[Unit]
Description=your description
After=network.target
After=local-fs-pre.target

[Service]
Type=oneshot
User=root
ExecStart=/usr/bin/bash /usr/bin/yourscript.sh

[Install]
WantedBy=multi-user.target

At the end

sudo systemctl enable yourscript.service

It should work, but I didn’t try it

Thanks RDaneel, that works perfectly. For reference sakes, the timing is in the order of miliseconds before the sound plays.

However I have encountered a slightly strange behavior. For simplicity, the script is simply pulling a LED high with python and RPi.GPIO. A few seconds after booting, the LED turns off. On the other hand if I run the script manually it stays on permanently (aka I only tested longer than a few minutes). I’m not sure if this has something with how the script is written or how it is initialized as a service. I’ve pasted the script’s code below, service file is exactly as linked.

[code]import RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

OPTO = 21

GPIO.setup(OPTO, GPIO.OUT)
GPIO.output(OPTO, True)[/code]

Bump back to front page

In the current version of volumio, the path of bash is
/bin/bash
Also, remember to set the permission of your script by
chmod +x /usr/bin/yourscript.sh