TechNazgul RSS

Tuesday, December 14, 2010

Using and autostarting irexec & irxevent with XBMC

The irexec application allows for customized scripts to be run based on input received from a remote control. When using XBMC running on Linux, this program can be very useful to do things like force a shutdown of the box, force an XBMC restart, etc. While XBMC itself has many remote functions built in, if XBMC hangs for some reason, having a remote-activated script to kill and restart XBMC is extremely handy.  If the reasons for doing this aren’t immediately apparent to you, imagine this post being renamed as “how to enable your wife-friendly XBMC kill switch.”

The problem is that getting irexec to work properly and to autostart after a reboot or standby is annoyingly difficult for people not deeply familiar with Linux (which includes me).  Adding to the complexity is the fact that irexec must be run as root for it to function properly.


Below are the steps I followed to allow irexec to autostart at boot and to keep running after resuming from standby as well.  I used other guides (this one in particular) as a starting point, and then had to branch off on my own to keep irexec running after going into standby and resuming.


Finally, a disclaimer. I am NOT a Linux expert.  This is the way I found to work for me, and I’m open to better ways to do it. If you have suggested improvements, let me know in the comments.


Step-by-Step Tutorial:


1. Make sure you have LIRC & irxevent

$ sudo apt-get install lirc

$ sudo apt-get install lirc-x


2. Create the .lircrc configuration file in your home directory

$ nano .lircrc


Below is an example .lircrc file that calls an XBMC restart script when the “Print” button is pressed on the remote.  More info on remote mapping can be found here, though in this case, using irexec takes care of all necessary mapping for us.


    prog = irexec
    button = Print
    config = /home/username/.xbmc/userdata/scripts/ &


Note that I’ve chosen to locate my script in my xbmc userdata folder, though you can put your script anywhere so long as you properly link to it.  I just like to keep it here as it’s one less thing to worry about losing when migrating between XBMC versions.


3. Create the script (in the location specified in #2 above)

(Note that killing a running command requires root access, which we’ll provide for later.)


# Kill XBMC and restart
echo "XBMC running, force restart."
sudo -u root /etc/init.d/xbmc-live stop

#Optional logging for troubleshooting.  Remove the hash to enable

#echo $(date) ": XBMC-live stop executed" >> /home/xbmc/.restart-log.log
sleep 2
sudo -u root /etc/init.d/xbmc-live restart

#Optional logging for troubleshooting.  Remove the hash to enable

#echo $(date) ": XBMC-live RESTART executed" >> /home/xbmc/.restart-log.log


4. Create your irexec restart script (in the location you link to in step #2).  Remember to replace all instances of xbmc in the script below with your own username (if it is not xbmc).




#filename /home/username/.xbmc/userdata/scripts/


# Test to see if irxevent is running first, if so kill it, then restart
if ps -ef|grep -v grep|grep -i irxevent
ps aux|grep -i xbmc|grep -i irxevent |awk '{print $2}'|xargs kill
# Do nothing
echo "irxevent already dead!"
# Test to see if irexec is running first, if so kill it, then restart
if ps -ef|grep -v grep|grep -vi start|grep -i irexec
ps aux|grep -i xbmc|grep -i irexec |grep -vi start|awk '{print $2}'|xargs kill
# Do nothing
echo "irexec already dead!"
#test to see if an instance of irxevent is already running
if ps -ef|grep -v grep|grep irxevent
# do nothing
echo "irxevent already running"
# start irxevent
irxevent /home/xbmc/.lircrc &
#test to see if an instance of irexec is already running
if ps -ef|grep -v grep|grep irexec
# do nothing
echo "irexec already running"
# start irexec

irexec -d /home/xbmc/.lircrc &


5.  Change the file properties to make your scripts executable. Run the following command in your scripts folder.


$ chmod +x *.sh


6. Allow these scripts to be executed without a root password prompt

If you don’t do this, the scripts will run and require a password, which will never work since you are going to be accessing them via remote control.  Do this at your own risk as obviously you are opening up some holes here allowing scripts to run without a password prompt. (though I believe the risk to be minimal since you are confining it to specific scripts)


Open up your sudoers file with this command:

$ sudo visudo


Add these line to the bottom of your file:

xbmc ALL=(ALL) NOPASSWD:/home/xbmc/.xbmc/userdata/scripts/

xbmc ALL=(ALL) NOPASSWD:/home/xbmc/.xbmc/userdata/scripts/


This allows the user xbmc (which you have to change if xbmc is not your user) to execute specific scripts without a password prompt.


6.  Test your restartxbmc script.  Run these command from your scripts folder.


$ sudo –k  (To kill your sudo rights and ensure we’re testing w/o sudo access)



(at this point, watch your display and XBMC should stop and restart)


Next, we will test our script to start/restart irexec.


7. Test your restartIR script.

First, see if irexec is running (which it probably is not):

$ ps aux|grep irexec 


Most likely, you’ll just get back a result showing your grep statement containing irexec, which is fine.  We just want to confirm whether it is or is not running before launching the script.


Now manually run the script.

$ sudo sh


Check again if irexec is running:

$ ps aux|grep irexec 

This time you should see:

root     24997  0.0  0.0   1780   220 ?        Ss   09:46   0:00 irexec -d /home/xbmc/.lircrc

Note that irexec must be running under the root user to work


(If this worked as planned, take a deep breath, you’ve accomplished more than half the task)


Now that we’ve done this much, we have to test that a remote button press is also capable of restarting XBMC.  For the remote button to execute the script, you will need to start the irexec daemon using the command above (if you didn’t already).


8. Take another deep breath, and press the Print button on your remote

This uses the .lircrc file we created in step 2 to call the script to restart XBMC.  When you press print, XBMC should be forcibly restarted.  If not, start at #1 and try again.  If you’re ready for more… read on.


<End of Tutorial, Part 1>


Great… so we have this much working… the next step, covered in a separate post, is how to get irexec to continue running after resuming from suspend and upon boot up.


Technorati Tags: ,,,


  1. Thanks for posting this, it's incredibly helpful! Couple of suggestions for you:
    1. instead of grep-ing the various processes from ps, you can alternatively use 'pidof', so something like 'pidof xbmc irxevent' and get a list of the pids to kill, which would be shorter and cleaner to read.
    2. Perhaps provide an alternative bash script for killing and restarting xbmc for those of us not using xbmc-live. I have a script I can provide if you would like it.

    Thanks again!

  2. This comment has been removed by the author.

  3. Hi I know this post is getting old but I'd apppreciate a copy of your bash script for killing and restarting xbmc.


  4. Ken - here is a link to the script in PasteBin. I still use it without issue so it should work.