我是 Mac OSX 新手,我正在尝试开发一个守护程序(我熟悉 Windows 服务,并试图实现相同的目标)。
到目前为止我做了什么:
设法在LaunchDaemons中编写一个plist文件(我发现这些守护进程在系统帐户下运行,而不是用户,就像具有本地系统权限的Windows服务一样)。
我设法让它在启动时运行,并运行我的简单的 hello world 应用程序,该应用程序每 10 秒向文件写入一些内容。
问题是,当我使用 launchd 卸载守护进程时,它只会杀死我的进程。 有什么方法可以让守护进程在卸载时运行其他内容,或者与我的进程通信它正在关闭,以便我可以在代码中执行卸载操作?
谢谢
最佳答案
Apple 记录了守护程序的生命周期,并描述了如何处理 Terminating Processes
如果您使用的是 Objective-c 或 Swift,您应该能够实现 applicationshouldterminate委托(delegate)方法。
如果您不使用 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 here. 的讨论
关于macos - Macos Launch Daemon 在卸载时运行某些内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46402689/