#!/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:
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.
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.
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.
@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.