I was running a rake task that launched a third party proprietary binary that handled some files. The process was usually fast, lasting about two or three seconds per file although sometimes it would just lock up and become unresponsive while still pegging the cpu at 100%.
While I love a good challenge, there are times when a battle proven solution is preferable.
Fortunetly for me, there was a *nix command named timeout, that did exactly what I was looking for.
The following would kill a running command with signal 9 (the equivalent of powering off the computer) if it runs longer than 30 seconds:
timeout --signal=9 30s /some/command
Suffice to say, that I was so pleased of this solution that I'm going to use it everytime I can.
A slight warning - the timeout command is provided by the coreutils package. While it was avaiable in Fedora, it wasn't on the staging machine I deployed it to, running CentOS. I did managed to get it working (without any problems) using the binary from my desktop machine.