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

112 Views Asked by At

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.)

2

There are 2 best solutions below

0
ahmd0 On BEST ANSWER

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.

1
Pratha On

Check the value of _NS_SYSTEM_RESTART key in the notify service.(use the launchd framework)