从LaunchDeamon捕获应用程序终止 [英] Catch App Terminate from LaunchDeamon
问题描述
我有一个可以作为LaunchDeamon运行的Cocoa基础工具。当应用程序被系统终止时(无论是通过重新启动还是关闭(甚至是launchctl卸载)),是否可以捕获此事件,以便执行某些完成功能?
I've got a Cocoa foundation tool that I run as a LaunchDeamon. When the app is terminated by the system, either by a reboot or shutdown (or even launchctl unload), is there a way I can capture this event so that I can perform some finalizing functions?
推荐答案
您正在讨论的所有案例都将 SIGTERM
发送到流程。您要为此添加一个信号处理程序。请参见信号的手册页a>和建议 。请仔细阅读警告。在信号处理程序中,只有某些函数可以合法调用(并且原则上您永远不要分配堆内存)。通常,最好只使用处理程序来设置一个标志,告诉您的主线程终止。
All the cases you're discussing send SIGTERM
to the process. You want to add a signal handler for that. See the man pages for signal and sigaction. Read the warnings carefully. Only certain functions are legal to call during a signal handler (and in principle you should never allocate heap memory). Generally it's best to just use the handler to set a flag that tells your main thread to terminate.
您可能还需要查看 PreLoginAgents 有关如何处理 SIGTERM $>的示例c $ c>使用运行循环(如果您使用的是运行循环)。
You may also want to look at PreLoginAgents for an example of how to handle SIGTERM
using the run loop, if you're using a run loop.
请参见终止进程有关将哪些信号发送到您的过程的完整详细信息的指南。
See Terminating Processes in the Daemons and Services Programming Guide for full details on what signals will be sent to your process.
这篇关于从LaunchDeamon捕获应用程序终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!