Search code examples
c++macoslaunchdrebootlaunch-daemon

How to tell from a launch daemon if macOS is currently restarting?


I have a launch daemon (written in C++) that runs on macOS. Is there a way to tell if the operating system is currently restarting?

(Or the moment after the user hit Apple Logo -> Restart and the OS is winding down.)


Solution

  • You can do it if you are willing to dig into undocumented (but open sourced) stuff. This blog post will show you an example, but basically you need to use notify_register_mach_port, CFMachPortCreateWithPort with some custom port names to register for notifications and then process them and remember last power event in a global state. I'll admit. Not as easy as it is on Windows.