LaunchDaemon 处理系统关闭 - 没有 SIGTERM?

发布于 2024-12-17 14:48:50 字数 421 浏览 0 评论 0原文

我编写了一个小的 python 脚本,将其作为 LaunchDaemon 运行,以将启动/关闭时间记录到远程数据库。当脚本启动时,它会记录启动时间,然后暂停并等待捕获 SIGTERM 以记录关闭时间。几乎相同的工作流程被用作登录/注销时间的启动代理。

然而,苹果的突然终止机制似乎正在给事情带来麻烦。看起来,当机器关闭或重新启动时,launchd仅向launchdaemons和launchagents发送SIGKILL信号,而无法处理该信号。我可能在技术细节上是错误的,但这本质上就是我所经历的。

使用 launchctl 手动加载/卸载守护进程会触发 SIGTERM 处理程序。但是,当真正的系统关闭发生时,不会触发相同的代码。

有人对如何防止 SIGKILL 有建议吗?

I've written a small python script that I'm running as a LaunchDaemon to record startup/shutdown times to a remote database. When the script launches it records the startup time, then pauses and waits to catch SIGTERM to record the shutdown time. An almost identical workflow is being used as a LaunchAgent for login/logout times.

However, it appears that Apple's Sudden Termination mechanism is throwing a wrench into things. It appears that when the machine is shutdown or restarted, launchd is only sending a SIGKILL signal to the launchdaemons and launchagents, which cannot be handled. I may be wrong in the technicalities, but that's essentially what I'm experiencing..

Manually loading/unloading the daemon with launchctl triggers the SIGTERM handler. However, that same code is not triggered when real system shutdown occurs.

Does anyone have a recommendation on how to prevent a SIGKILL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2024-12-24 14:48:50

如果我正在阅读 这个对,它不应该在守护进程上使用突然终止,除非你有launchd plist 中的EnableTransactions。但文档有点模糊,所以我还尝试将 EnableTransactions 添加到 plist,或者如果这不起作用,请调用 vproc_transaction_begin从你的程序(我假设你可以从python调用它,但我不知道机制)。

If I'm reading this right, it shouldn't use sudden termination on daemons unless you have <key>EnableTransactions</key><true/> in the launchd plist. But the docs are a little vague, so I'd also try adding <key>EnableTransactions</key><false/> to the plist, or if that doesn't work call vproc_transaction_begin from your program (I assume you can call it from python, but I don't know the mechanics).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文