LaunchDaemon 处理系统关闭 - 没有 SIGTERM?
我编写了一个小的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正在阅读 这个对,它不应该在守护进程上使用突然终止,除非你有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).