开发者

Automatic timeout for launchd jobs

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 qu开发者_运维知识库ite what I'm looking for.


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.


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)


(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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜