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

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.



#file => /etc/pm/sleep.d/99lirc-resume

# This script will restart LIRC and IREXEC upon resume.

. /usr/lib/pm-utils/functions


case "$1" in

        /etc/init.d/lirc stop

         /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


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/


This is an example of a cron job that runs our 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: ,,,

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: ,,,

Monday, December 13, 2010

Windows – How to restore windows that appear off screen

This falls into the category of frequent and annoying problems! I end up Googling this every time it happens, and here is how you fix it. This works even if you use a multi-monitor task bar like DisplayFusion offers.


  • Use ALT+TAB to switch to the off-screen application.
  • Press ALT-SPACE to bring up the system menu (you won't see it because it is off screen)
  • Press R to select the "Restore" menu choice to ensure the windows isn't is maximized (you can not move it if it is maximized)
  • Press ALT-SPACE again, then M to select the "Move" menu choice.
  • Press one of the arrow keys to initiate the movement.
  • Now just use the mouse to place the window where you want.



Technorati Tags: ,