启动作业的自动超时
我有一个已配置为运行单元测试的启动作业。有时,单元测试会挂起(通常是因为一些错误的代码)。我知道我的测试通常只需要几分钟即可运行。当它们挂起时,我通常在大约一个小时内不会注意到它,此时我必须打开活动监视器并手动强行终止我的测试。我希望 launchd 为我做这件事。如何让 launchd 在一定时间后自动终止我的作业(如果它仍在运行)?
我一直在查看 launchd 手册页,我注意到一些有希望的键(例如 TimeOut
和 ExitTimeOut
),但描述中的措辞似乎不太完全我在寻找什么。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
launchd 无法了解或关心您的进程所做的任何事情。只要几秒钟没有退出,那就幸福了。
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.
在使用了
TimeOut
和ExitTimeOut
之后,我得出的结论是,launchd 似乎没有办法在一定时间间隔后终止进程。我最终将所需的代码添加到我的程序本身中。 (我的程序不仅仅进行单元测试)
After playing with
TimeOut
andExitTimeOut
, 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)
(注意:这些是半原始的想法,而不是完美的解决方案。)
一种可能性是使用 launchd 的看门狗功能。如果您可以自定义单元测试以在启动时接触文件,则可以在检测到该文件时启动一个单独的 launchd 任务。如果单元测试脚本没有在指定的时间间隔内删除该文件,那么应该将其杀死。为此,您可以让看门狗任务启动一个脚本来获取单元测试的 PID,休眠所需的时间间隔,然后在该进程仍在运行时终止单元测试。 YMMV。
(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.