TechNazgul RSS

Wednesday, December 15, 2010

Autostarting irexec & irxevent with XBMC, part two



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. 

 

  1. Auto-starting the irexec daemon on boot, and
  2. 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.

 

Technorati Tags: ,,,

5 comments:

  1. Does it work the same way on ubuntu 11.04? Because I can't get irexec to start at boot, nor to start with cron. Although the command "sudo -u root irexec -d /home/xbmc/.lircrc &" works normally if I use it in the terminal.

    ReplyDelete
  2. Sorry to say that I don't know. Getting irexec to work properly is an absolute nightmare and it took me a long time to figure out this much, so I feel your pain. If you figure it out, please post back.

    ReplyDelete
  3. Not totally sure of ubuntu 11.04 but on mythbuntu 11.04 I did the following to start irexec on boot.
    In /etc/init.d/lirc set START_IREXEC=true at ~line 111.
    In /etc/lirc/lircrc delete #UNCONFIGURED and add include statements for each application specific lirc mapping as required.

    ReplyDelete
  4. In Ubuntu 11.10 Startup Applications is readily available in System Menu (top right corner of the screen, where shutdown/suspend/etc is).

    ReplyDelete
  5. You do not want to start the irexec daemon (or similar) as root. You enable anyone to execute statements as root without needing the root password.

    ReplyDelete

Followers

Facebook