Run script at boot before kodi launchs

Hi,

I have a small script, which is waking my file sever and mounts the .kodi profil folder. This needs to be run before kodi is started.

On Raspbmc I put the line for executing the script to /etc/init/kodi.conf in pre-start script section. Which works great.

Since OSMC is using systemd, I don’t know how to restore the old behaviour. I’m not familar with systemd and need some advice.

rc.local does not work, because it is not waiting util the folder is mounted from network before kodi starts.

I’m grateful for any help.

will;

I’m using cron to start scripts at boot.

osmc@osmc:~$ crontab -l
@reboot /usr/bin/python /usr/local/bin/deluged
@reboot /usr/bin/python /usr/local/bin/flexget daemon start -d
@hourly /bin/bash /home/osmc/checkrunning.sh

There are quite a few different ways you could approach this with systemd.

One is you could create a new independent service unit that had a Before=mediacenter.service declaration in it, that would cause the mediacenter service (/usr/bin/mediacenter which launches kodi) to be delayed until your service finished and exited.

A much simpler approach would be to add an ExecStartPre= line in the service unit for mediacenter at /lib/systemd/system/mediacenter.service:

http://www.freedesktop.org/software/systemd/man/systemd.service.html

This is not recommended because when we update the Kodi package your changes would be overwritten. However systemd has a neat feature called “drop-ins” which let you override or add additional lines to systemd units in a separate file that won’t be overridden:

http://www.freedesktop.org/software/systemd/man/systemd.unit.html

(Example 2. Overriding vendor settings)

So what you would do is create a file called

/etc/systemd/system/mediacenter.service.d/local.conf

And in that file you would put just:

[Service]
ExecStartPre = /path/to/my/script

This line effectively gets merged with whatever we have in /lib/systemd/system/mediacenter.service and your changes won’t be lost during upgrades.

With an ExecStartPre line you must specify the full path to the executable/script and it doesn’t execute within a shell - eg the script itself must have #!/bin/bash or similar at the start or you would need to call it via a shell.

Keep in mind your script must return a success (0) return code otherwise the service unit will be aborted as failed and Kodi won’t load.

1 Like

Can you explain this a little more? How would I know what return code a script is giving?

The return code of a bash script is the return code of the last command run in the script, or if you use exit you can put the return code after exit, such as exit 0 to explicitly set the return code.

I mention it because in many cases the return code of a script will be ignored but any script run by systemd when starting a service has to succeed (return code 0) for the service as a whole to succeed in starting up.

#!/bin/bash
echo " " >> ~/.flexget/flexget.log
echo "`date +%Y.%m.%d`, `date +%l:%M%p`: Checking to see if flexget & deluge are running..." >> ~/.flexget/flexget.log
echo "Flexget:" `ps aux|grep "[b]in/flexget" | awk -F python '{print $2}'` >> ~/.flexget/flexget.log
echo "Deluge :" `ps aux|grep "[b]in/deluged" | awk -F python '{print $2}'` >> ~/.flexget/flexget.log
echo " " >> ~/.flexget/flexget.log

exit 0

I just added exit 0 to the end of my simple script. It’s that simple?

exit 0 at the end (assuming your script only ever exits at the end of the script) will always return a success code even if some commands in the script failed, yes.

That might or might not be what you want depending on circumstances.

Thanks @DBMandrake the Overriding vendor settings method works great.