Start mythtv from remote control

I’ve been trying to find a way to start mythtv from the remote control for quite a while now. While I love Mythtv, it is definitely not ready for mainstream yet. I don’t mind tinkering with stuff – ok, I actually really enjoy it. My family however, does not appreciate it when something goes wrong and I’m not there to fix it.

It’s rare when the frontend crashes, but it does happen. My usual solution is just to vnc into the box and restart it (no keyboard or mouse, it’s an htpc hooked up to our tv). Although not too difficult, that’s really more of a pain than you can reasonably expect your family to endure.

First, I had to figure out why lirc wasn’t working every time the system rebooted. After much searching, it turns out I had to comment out the following lines in /lib/udev/rules.d

ACTION=="add", KERNEL=="lirc[0-9]", RUN="/etc/init.d/lirc restart udev"
ACTION=="remove", KERNEL=="lirc[0-9]", RUN="/etc/init.d/lirc stop udev"

Upon rebooting, that solved the problem of no lirc. That was a relief, because prior to this edit, I had to restart lirc and then restart the front end. That’s just a pain.

I went about things a little backwards and went ahead and mapped a button to a script that was executed via irexec. I added the following to my ~/.lircrc

begin
        remote = mceusb
        Button = Blue
        prog = irexec
        repeat = 0
        config = /usr/local/bin/startmythfrontend.sh
end

Naturally, you’ll want to change that to whatever matches your remote, button and script name that you’re using.

After a great deal of searching, experimentation, and learning how to use “if” in a bash script, I came up with the following script:

#!/bin/bash
if [ $LOGNAME == "root" ]; then
echo "Hello root, you should be running this script as \"mythtv\" user, goodbye.";
exit
fi

if [  $(pidof mythfrontend.real) ]; then
echo "mythfrontend is already started"; else
echo "starting mythfrontend"
DISPLAY=:0 mythfrontend > /var/log/mythtv/mythfrontend.log 2>&1 &
fi

I modified a script on the mythtv wiki to come up with one to meet my needs. I saved it as startmythfrontend.sh and did a

sudo chmod +x startmythfrontend.sh

After that, I realized that this wasn’t going to do me any good if I couldn’t get an irexec daemon to always run. I tried messing with the init.d, but my script writing skills aren’t quite up to that point yet. Instead, I just added this line at the beginning of /usr/bin/mythfrontend

/usr/local/bin/startirexec

Then, I wrote the following script, named it startirexec and then chmodded it +x :

#!/bin/bash
if [ ! $(pidof irexec) ]; then
echo "starting irexec daemon"
irexec -d
else echo "irexec already started"
fi

Now whenever I press the right button on my remote, the frontend starts if it isn’t started, and nothing happens if it’s already started.

I’m a beginning scripter, so let me know if there’s anything I can do more efficiently or elegantly.