Way to make launchd stop a service on sleep and start it up again on wake

launchdsleep-wake

I have several pieces of server software installed on my MacBook Pro for work using Homebrew. There is one service in particular, that likes to peg the CPU after the OS wakes up from sleep until I restart it. The service is currently managed by launchd, and I am wondering if there is a way for me to tell launchd to stop the service when the OS goes to sleep, and then start it up again on wake.

Best Answer

You can use SleepWatcher:

It can be used to execute a Unix command when the Mac or the display of the Mac goes to sleep mode or wakes up, after a given time without user interaction or when the user resumes activity after a break or when the power supply of a Mac notebook is attached or detached.

Run launchctl load <path> and launchctl unload <path> with SleepWatcher on wake and sleep respectively.