Neat little logic 'hack': remote control power commands. Also, monitoring and auto-restarting an unreliable daemon

This is the kind of thing I just love. Now I have my PVR setup, it's possible for the power state of the cable box to get kind of 'out of sync', with the combination of two different 'setups' - direct TV, and PVR - that use it, and my Harmony remote control for controlling both through 'activities'. The box could wind up off when the Harmony thinks it's on, or vice versa.

Frustratingly, just like many many devices these days, my box (a Motorola 6200, remember) doesn't have separate Power On and Power Off functions (either in hardware or IR codes). It just has a power toggle button, which turns it on if it's off, and off if it's on. This becomes a bit tricky; I want to make sure it's always on and off at the right times, particularly when you just want to sit down and press one button and use it, and when MythTV wants to record stuff.

The MythTV side of the equation turned out to be simple: there's a neat channel changer script for MythTV called 'mythchanger' which supports switching the box on before changing channels - it doesn't toggle the power, it is able to detect whether it's on or off, so it turns it on if it's off and leaves it on if it's on. The Firewire interface must support that. Neat. That covers MythTV, as MythTV always runs the 'channel changer' script before watching or recording TV. (This one also doesn't need you to specify the Firewire node of the box - it goes off the box's UUID, or just picks the first box it finds. So it works without reconfiguration when my box decides to change nodes. Yay!)

That left the Harmony side of the equation. If you just use the wizard setup, it depends on the remote tracking whether the box is on or off, which isn't going to be reliable now MythTV can potentially change it. The Harmony setup is capable of setting up discrete Power On and Power Off commands and then using them appropriately, but my box doesn't have any! What now?

I luckily found a somewhat obscure forum thread where one Hailey Williamson (no relation) is credited with a great little logic hack. She noticed that, if the box is off and you hit Menu, it will be turned on (and sent straight into the menu system). If the box is turned on and you hit Menu, the menu system comes up. So you can now derive a simple little logic hack: the command string MENU POWER will always turn the device off, whether it's on or off to start with (if it's off, it turns it on (and into the menu) then immediately off again; if it's on, it goes to the menu then turns it off). It follows of course that the command string MENU POWER POWER will always turn the device on. So you just define those command strings as the Power On and Power Off buttons in the Harmony configuration tool (luckily it supports that kind of hackery). Incredibly simple, but I doubt I'd ever have thought of that on my own. Cool trick! I set mine up that way and it works perfectly.

Another little tweak I set up today is to monitor the mythbackend process (that's the MythTV 'server'). It's known to not be really super-reliable; every so often it does fall over, for one reason or another. Obviously you don't want that to happen to a PVR setup. So I found a reference to using a neat little tool called Monit to monitor it. Monit is a fairly powerful generic monitoring tool for *nix systems; it can monitor all sorts of things in different ways and perform actions depending on what it sees. It can be run as a one-time check or as a daemon which checks all the things it's set to monitor at regular intervals. So with monit running as a daemon and the following configuration:

check process mythbackend with pidfile /var/run/mythbackend.pid start program = "/etc/init.d/mythbackend restart" stop program = "/etc/init.d/mythbackend stop" if failed host localhost port 6544 then restart if 5 restarts within 5 cycles then timeout

(thanks Thomas Mashos!), every time a monit scan happens, it checks if mythbackend is currently supposed to be running (by checking for the pidfile), and if it is, checks if it really is (by trying to poke the port it should be listening on). If it's not there, it restarts it. If it fails the check five consecutive times, it figures there's something really wrong - it's not just randomly falling over - and gives up until you poke it manually. monit even does email notification, so you'll know when it's falling over and when it hits timeout. Really neat little tool.

Comments

No comments.