Using “new” launchctl subcommands to check for and reload launch daemons

Note

This is a follow-up post to launchctl “new” subcommand basics for macOS

Legacy Subcommands

With the legacy subcommands for launchctl, you could check if a launch daemon was running by getting the output of

sudo launchctl list | grep LAUNCHDAEMONLABEL

And then you could just launch it up by running:

sudo launchctl load -w /Library/LaunchDaemons/NAMEOFLAUNCHDAEMONFILE.plist

Non-Legacy Subcommands

With the “new” (non-legacy) subcommands for launchd, there’s the added complication of launch daemons being not only unloaded but also possibly disabled. Also, if you run

launchctl print system

it prints everything that’s running but also includes all the disabled launch daemons.

Get Disabled Launch Daemons

You can get what’s disabled by running

launchctl print-disabled system

The only problem with that is it will print both disabled and enabled, so you can cheat a bit by filtering on only the disabled ones:

launchctl print-disabled system | grep disabled

Get a Specific Launch Daemon’s Load Status

Rather than parsing through the output of launchctl print system to get what’s running, you can look at the state of a specific launch daemon instead.

Regardless of how you check, keep in mind this warning from man launchctl:

IMPORTANT: This output is NOT API in any sense at all. Do NOT rely on the structure or
information emitted for ANY reason. It may change from release to release without
warning.

That said, it is a lot easier to figure out what’s going on based on the output of

launchctl print system/LAUNCHDAEMONLABEL than it is to try to parse the output of launchctl print system to figure out if LAUNCHDAEMONLABEL is loaded.

If you run

launchctl print system/LAUNCHDAEMONLABEL

and get back

Bad request.
Could not find service "LAUNCHDAEMONLABEL" in domain for system

that means the launch daemon isn’t loaded.

Important distinction: if it is loaded, you may see in the output

state = not running

or

active count = 0

That’s fine. It may not be actively running at the very moment you’re checking, but if you’re getting output back, the launch daemon is loaded. “Loaded” doesn’t mean “always running.”

Example Script

Just so you can see a more practical example, I’ve thrown together a sample script of how to use an associative array in zsh to check for and re-enable or re-load various launch daemons: Reload_LaunchDaemons.sh


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *