Mac OSX LaunchDaemon 运行 正在卸载一些东西

Mac OSX LaunchDaemon run something on unload

我是 Mac OSX 的新手,我正在尝试开发一个守护进程(我熟悉 windows 服务,并试图实现相同的目标)。

到目前为止我做了什么:

设法在 LaunchDaemons 中写入一个 plist 文件(我发现那些守护进程 运行 在系统帐户下,而不是用户,就像具有本地系统权限的 windows 服务一样)。

我设法在启动时做到了 运行,并且 运行 我的简单的 hello world 应用程序每 10 秒就向文件写入一些内容。

问题是,当我用 launchd 卸载守护进程时,它只会终止我的进程。 是否有任何方法可以让守护进程 运行 卸载其他东西,或者通知我的进程它正在关闭,以便我可以在我的代码中执行卸载操作?

谢谢

Apple 记录了守护进程的生命周期,他们描述了如何处理 Terminating Processes

如果您使用 Objective-c 或 Swift,您应该能够实现 applicationshouldterminate 委托方法。

如果您不使用 Cocoa:

Non-Cocoa applications receive a “Quit Application” Apple event (kAEQuitApplication), as a courtesy, to give them a chance to shut down gracefully

最后,由于您是 运行 守护进程(后台进程)

For background processes, the procedure is a little different. The loginwindow program notifies the process that it is about to be terminated by sending it a Quit Application Apple event (kAEQuitApplication). Unlike foreground processes, however, loginwindow does not wait for a reply. It proceeds to terminate any open background processes by sending a SIGKILL signal, regardless of any returned errors.

If the system is being shut down or restarted, it sends a SIGTERM signal to all daemons, followed a few seconds later by SIGKILL signal.

有关于处理 SIGTERM 的讨论