Now that we’ve set up our scripts for restarting XBMC and irexec/irxeevnt, we need to go about ensuring that irexec is running at all times so that the scripts work when executed via remote. There are two situations we need to plan for.
- Auto-starting the irexec daemon on boot, and
- Restarting the daemon after resuming from suspend
In addition, we’ll discuss a way to periodically check if irexec has failed and recover if necessary.
1. Autostarting irexec on boot up.
This is done by placing your irexec start script in the /etc/init.d directory. From within that directory:
$ sudo nano irexec
Then, copy the following into your script (replace xbmc with your username where necessary):
#! /bin/bash
sudo -u root irexec -d /home/xbmc/.lircrc &#Optional logging, remove hashes to troubleshoot.
#echo $(date) ": XBMC-etc.init.d script executed" >> /home/xbmc/etc-init.d-irexec.log
exit
Next, use update-rc.d to configure this script to run on boot.
$ sudo update-rc.d irexec defaults
This should set up irexec to autostart on boot and run as root (which is necessary for it to work correctly).
2. Restarting the irexec daemon after resuming from suspend
Now that we have irexec starting on boot, we also have to set up Linux so that it restarts the program after it has been suspended (as otherwise it does not start itself). The way to do this is through use of the scripts in this directory: /etc/pm/sleep.d
In this directory, you will likely see a few other scripts and can pull one out as an example of what we need to accomplish for irexec. In XBMC-Live, a script already exists called 99lirc-resume, which we can edit with one additional line to start up irexec as well.
Below is the modified 99lirc-resume script I’m using. The important part is in the thaw|resume section, where we tell the OS to start up lirc and irexec upon restore from standby. Note here again that you have to call irexec as root for it to function properly. Also remember to replace xbmc with your own username.
#!/bin/bash
#file => /etc/pm/sleep.d/99lirc-resume
#
# This script will restart LIRC and IREXEC upon resume.. /usr/lib/pm-utils/functions
case "$1" in
hibernate|suspend)/etc/init.d/lirc stop
;;
thaw|resume)/etc/init.d/lirc start
sudo -u root irexec -d /home/xbmc/.lircrc &
# Optional logging below for troubleshooting. Remove the hash to enable.
# echo $(date) ": XBMC-hibernate script executed" >> /home/xbmc/.lirc-resume.log
;;
*)
;;esac
exit $?
Once this is in place, you can test it by manually killing irexec (sudo pkill irexec) and then suspending and resuming. You should see irexec running as root when you resume.
3. Using cron to check for irexec (and recover if needed)
This step is optional, but it’s another wife-proofing step that I feel better having in place. Using cron, we can check periodically (as frequently as every minute up to any frequency you desire) that irexec continues to run in the background.
To do so, we call up crontab for the root user (since irexec must be run as root, but you knew that by now, right?) :)
$ sudo crontab –e –u root
(choose nano for your editor if prompted)
Within cron, add this (change your username if needed):
# m h dom mon dow command
0-59/5 * * * * /home/xbmc/.xbmc/userdata/scripts/restartIR.sh
This is an example of a cron job that runs our restartIR.sh script every five minutes. (This is the script we created in the previous post to check if irexec is running, and if not, fire up an instance). If you wanted to run it every hour, you’d change the “0-59/5” to “0-59/1” and voila.
And that’s it, with these 3 steps in place, you should have a nearly bullet-proof irexec implementation. Once you have the irexec daemon running reliably, you can use your remote to issue any variety of other commands. For example, you might want to enable a hard reboot command via remote to allow recovery from serious system crashes, etc. Or, maybe you’d like to switch between XBMC & MythTV via remote. The possibilities are endless once you have the basic framework in place.