Blog Archive

Friday 15 March 2013

How to reboot Openelec when it wakes from sleep

Update: XBMC v13 (Gotham) has changed the way that the wake routines work slightly. The .power script needs to be in the same place, but instead of the 1st parameter passed to the script being "suspend" or "resume", they are "pre" and "post" respectively.  No idea why. The reboot command has also moved from /sbin to /usr/sbin. With that in mind, here is how my script now looks:

#!/bin/bash
echo "$(date) $1" >> /storage/WAKE.log
if [ "$1"  = "post" ]
then
echo "$(date) $1 rebooting..." >> /storage/WAKE.log
/usr/sbin/reboot
fi

------------

I have a Zotac zbox running OpenElec (2.99.5 as I write this - I'm waiting patiently for the final version 3). I like to have it go into standby when I'm not using it. So far, so easy: I just set XBMC to issue a suspend instead of shutting down when receiving a power off command from my MCE remote. This is achieved in the XBMC settings menu.
There was a problem with this however. I have my media served to the zbox over NFS. I find NFS to be less CPU intensive on both the zbox and the Linux Mint server which hosts my RAID array.
The problem was that sometimes when the zbox came back out of sleep when instructed to do so by the remote it wouldn't be able to see the NFS shares for some reason and the only way to get it working again was to reboot the zbox. Obviously this did not pass the "wife test". It annoyed her and made her just switch everything off and not bother.

So the first thing I did was to put a small PHP web page on the server which simply SSHed into the zbox and issued a reboot. I made this look OK on an iPhone and added an icon to the wife's iPhone home screen.

"reboot" issues '/sbin/reboot'
"restart" issues '/bin/killall -9 xbmc.bin'


This was still a little untidy, as there were extra steps to get things working.
So instead I looked for a way to reboot the zbox when it was woken from sleep.

There is a way. It turns out that OpenElec (or XBMC, not sure which) automatically executes any script with the extension .power found in the directory  /storage/.xbmc/addons/_server/sleep.d/
I put a script called wake.power in there which looks like this:


#!/bin/bash
echo "$(date) $1" >> /storage/WAKE.log
if [ "$1"  = "resume" ]
then
  echo "$(date) $1 rebooting..." >> /storage/WAKE.log
  /sbin/reboot
fi



And, hey-presto! When the zbox is woken from sleep it reboots, making it look like it has actually just been powered on. This works well because OpenElec only takes a few seconds to display the interface after a reboot.