How to run script at boot? [SOLVED]

I’m trying to run texturecache.py at statup and am having problems getting it to work.

I can run this from the command line:

osmc@osmc:~$ /usr/bin/python /home/osmc/texturecache.py @logfile= @xbmc.host=localhost @bin.tvservice=/opt/vc/bin/tvservice rbphdmi &

but when I add it to /etc/rc.local or /etc/init/texturecache.conf as the following:

#Script for starting texturecache

description "Texturecache Service"

start on startup

respawn

exec /usr/bin/python /home/osmc/texturecache.py @logfile= @xbmc.host=localhost @bin.tvservice=/opt/vc/bin/tvservice rbphdmi &

it doesn’t seem to be running. I’ve searched all over the net for ways to auto-start this script with no luck so far. Thanks for any help!

OSMC does not use Upstart, so /etc/init/.conf files will not do anything. You should look at systemd services instead.

rc.local does work, but you probably need to chmod +x it

    osmc@osmc:/etc$ ls -al|grep rc.local
-rwxr-xr-x  1 root root     443 Apr 30 04:00 rc.local

My current rc.local:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

su - osmc -c "/usr/bin/python /home/osmc/texturecache.py @logfile= @xbmc.host=localhost @bin.tvservice=/opt/vc/bin/tvservice rbphdmi &"

exit 0

Any suggestions VERY MUCH appreciated.

EDIT: I’m having some luck with systemd. The only problem is I need the service to start after Kodi has loaded. Right now I have this:

[Unit]
Description=Texturecache Daemon
After=syslog.target network-online.target

[Service]
User=osmc
Group=osmc

Type=simple
ExecStart=/usr/bin/python /home/osmc/texturecache.py @logfile= @xbmc.host=localhost @bin.tvservice=/opt/vc/bin/tvservice rbphdmi 1800
TimeoutStopSec=20

[Install]
WantedBy=multi-user.target

How can I get it to start later? Or pause/sleep until Kodi has loaded?

You could make sure it starts after the Mediacenter service by adding mediacenter.service to your After line in the Unit section, but that doesn’t mean that Kodi has finished starting up, it just means that /usr/bin/mediacenter (the script that runs and monitors Kodi) has started.

If Kodi must be fully up and running to the home screen before you run your script you’re kind of in trouble - because there is no simple way to do that in a service definition.

You would have to probe/query Kodi using json rpc or something similar in a sleep loop to find out when it has finished starting up, or… just put a big delay and hope for the best…

I opted for the big delay. Seems to be working alright so far.

Only problem I’ve noticed is it seems to be using this method to turn off HDMI:

This is not an addon or a screensaver, it’s a script (daemon) that monitors JSON notifications and when it sees that the screensaver has been activated it starts a timer that will, when it elapses (default 900 seconds), remove HDMI power with a call to “tvservice --off”.

If the screensaver is deactivated before the timer has elapsed, the timer is cancelled and the system continues normally.

When the screensaver is deactivated through user activity and HDMI power is off, the HDMI power will be restored (tvservice --preferred) followed by a JSON call to Application.Quit() so that XBMC terminates and is automatically restarted - not rebooted - and the EGL context restored. This takes about 15 seconds from deactivating the screensaver to the menu re-appearing on a 1GHz Pi running a nightly build of OpenELEC Gotham.

But when I run texturecache.py from the command line it uses this updated method:

When using Raspberry Pi firmware from 23 May 2014 onwards (check with vcgencmd version), “vcgencmd display_power 0” and “vcgencmd display_power 1” will be used to disable/enable HDMI as this method does NOT require xbmc.bin to be restarted. See this post for further details.

I’m not sure what difference the method of starting the script effects how the script runs, but that seems to be what’s happening in my case.

texturecache.py script can be found here if anyone wants to take a look at it.

No, but autostart.py would work great for this

S

Is this available for OSMC? Got a link?

Just put a Python script in your Kodi directory named as autostart.py

Sam

My Solution:
Install texturecache.py:

wget -q http://is.gd/XGXbTG -O ~/texturecache.py
chmod +x ~/texturecache.py

Create systemd timer & service files:

/etc/systemd/system/texturecache.timer

[Timer]
OnBootSec=60

[Install]
WantedBy=multi-user.target

/etc/systemd/system/texturecache.service

[Unit]
Description=Texturecache Daemon
After=mediacenter.service texturecache.timer

[Service]
User=osmc
Group=osmc

Type=simple
ExecStart=/usr/bin/python /home/osmc/texturecache.py @bin.vcgencmd=/opt/vc/bin/vcgencmd @logfile=/home/osmc/texturecache.systemd.log @xbmc.host=localhost @bin.tvservice=/opt/vc/bin/tvservice rbphdmi 1800 &
TimeoutStopSec=20
Restart=on-failure
RestartSec=60
StartLimitInterval=60s


[Install]
WantedBy=multi-user.target

Both timer and service need to be enabled:

sudo systemctl enable texturecache.service
sudo systemctl enable texturecache.timer

If everything works right, the texturecache.timer waits 60 seconds after boot, then starts texturecache.service, which runs texturecache.py! This is my first time working with systemd so if you see something that needs fixing, please post it so I can fix mine.

4 Likes

Is it possible to use Partymode Autostart addon too.
In the configuration enable playlist and browse where the video reside.
http://kodi.wiki/view/Add-on:Partymode_autostart
http://forum.kodi.tv/showthread.php?tid=203290

Thank you so much for this!

I spent a few hours trying to figure this solution out on an old TV that has poor CEC support; the normal CEC standby and wake settings in kodi would never put my TV into standby. Once I found this post, I was implemented, tested, and happy within about 10 minutes. This is something that should be built into RPi versions of Kodi or at least OSMC.

This is why I love the Pi and Linux! So flexible.

1 Like

Best work ever! Thanks a lot! Exactly what i was looking for!

1 Like

@sam_nazarko Once in a while the service/script will fail. Not really sure what causes it but I suspect it’s got something to do with kodi restarting after updates.

Could you suggest improvements to the systemd .service so that it would be persistent? (trying to restart the service after failure?)

Seems like a few people are finding this useful, so if I can get that last bug worked out I’ll put it into a HOW-TO.

This is what it looks like when it fails: (I exited kodi for a few minutes just now to cause this.)

osmc@osmc:~$ sudo systemctl status texturecache
● texturecache.service - Texturecache Daemon
   Loaded: loaded (/etc/systemd/system/texturecache.service; enabled)
   Active: failed (Result: exit-code) since Sat 2015-10-03 03:55:37 EDT; 5s ago
  Process: 24686 ExecStart=/usr/bin/python /home/osmc/texturecache.py @bin.vcgencmd=/opt/vc/bin/vcgencmd @logfile=/home/osmc/texturecache.systemd.log @xbmc.host=localhost @bin.tvservice=/opt/vc/bin/tvservice rbphdmi 3600 & (code=exited, status=1/FAILURE)
 Main PID: 24686 (code=exited, status=1/FAILURE)

Oct 03 03:55:37 osmc python[24686]: rbphdmi(delay=_delay)
Oct 03 03:55:37 osmc python[24686]: File "/home/osmc/texturecache.py", line 6979, in rbphdmi
Oct 03 03:55:37 osmc python[24686]: MyJSONComms(gConfig, gLogger).sendJSON({"method": "JSONRPC.Ping"}, "libListen", callback=rbphdmi_listen, checkResult=False)
Oct 03 03:55:37 osmc python[24686]: File "/home/osmc/texturecache.py", line 2042, in sendJSON
Oct 03 03:55:37 osmc python[24686]: s = self.getSocket()
Oct 03 03:55:37 osmc python[24686]: File "/home/osmc/texturecache.py", line 1970, in getSocket
Oct 03 03:55:37 osmc python[24686]: raise lastexception if lastexception is not None else socket.error("Unknown socket error")
Oct 03 03:55:37 osmc python[24686]: socket.error: [Errno 111] Connection refused
Oct 03 03:55:37 osmc systemd[1]: texturecache.service: main process exited, code=exited, status=1/FAILURE
Oct 03 03:55:37 osmc systemd[1]: Unit texturecache.service entered failed state.

I’m on the edge to go crazy!
I just can’t get systemd to start inadyn-mt, when manually starting it there are no problems.

[Unit]
Description=start ddns-client inadyn-mt
After=mediacenter.service inadyn-mt.timer

[Service]
User=osmc
Group=osmc
Type=simple
ExecStart=/usr/local/bin/inadyn-mt

[Install]
WantedBy=multi-user.target

When checking with “systemctl status inadyn-mt.service” it shows

* inadyn-mt.service - start ddns-client inadyn-mt
   Loaded: loaded (/etc/systemd/system/inadyn-mt.service; enabled)
   Active: inactive (dead) since Sat 2015-11-28 16:48:49 CET; 11min ago
  Process: 480 ExecStart=/usr/local/bin/inadyn-mt (code=exited, status=0/SUCCESS)
 Main PID: 480 (code=exited, status=0/SUCCESS)

Any help would be greatly appreciated!!

Posting in a “[SOLVED]” thread is probably not going to get you much help. Start a new thread for this, maybe?