views:

180

answers:

3

I have a launchd job that I've configured to run my unit tests. Every now and then, the unit tests will hang (usually because of some bad code). I know that my test usually only take a couple minutes to run. When they do hang, I usually don't notice it for about an hour, at which point I have to pop open Activity Monitor and forcibly kill my tests manually. I want launchd to do this for me. How can I have launchd automatically kill my job (if it's still running) after a certain amount of time?

I've been looking over the launchd man page, and I've noticed a couple promising keys (like TimeOut and ExitTimeOut), but the wording on the descriptions seems like it's not quite what I'm looking for.

A: 

After playing with TimeOut and ExitTimeOut, I've concluded that there doesn't seem to be a way for launchd to kill processes after a certain time interval.

I ended up adding the needed code to my program itself. (My program does more than just unit tests)

Dave DeLong
+1  A: 

launchd has no way of knowing or caring about anything your process does. As long as it goes a few seconds without exiting, it's happy.

Azeem.Butt
A: 

(Note: These are semi-raw ideas, not a polished solution.)

One possibility is using the watchdog functionality of launchd. If you can customize your unit tests to touch a file when it starts, you could have a separate launchd task start up when it senses that file. If the unit test script doesn't delete the file within a specified time interval, then it should be killed. To do this, you could have the watchdog task launch a script that gets the PID of your unit tests, sleep for the desired interval, then kill the unit tests if that process is still running. YMMV.

Quinn Taylor
This is similar to what I ended up implementing, except that mine runs as a second thread within my program.
Dave DeLong