macOS launchd Service Management


Some time ago, I posted about starting Apache automatically when macOS 10.12 Sierra starts, using the launchctl command to add Apache to the launchdLaunch Daemon/Agent Manager. For those familiar with Windows, this is akin to managing Windows Services.

Previously, I mentioned that to automatically start Apache, run the command:

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

And to stop Apache from auto-starting:

sudo launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist

Note that there is no output most of the time so it's hard to confirm you got it right!

As can be seen, launchctl gets the definition from a .plist to add Apache as a "daemon". A daemon serves all users on the machine system-wide (a bit like the Windows registry HKLM runs services for the "Local Machine"), while agents run per-user (a bit like HKCU for the "Current User").

To list all launch daemons and agents, use launchctl list, but since I want to ignore any Apple provided processes, I use:

launchctl list | grep -v apple

I get the list below. Note that some items are running processes manually started (e.g. Firefox in my case), and other items are started via System Preferences > Users & Groups > Login Items (e.g. itsycal in my case). The PID - means that the job is loaded but not running (e.g. only the jamf agent is running in my case):

PID Status  Label
12345   0   org.mozilla.firefox.9876
-   0   org.mozilla.firefox.8765
-   78
-   0   com.openssh.ssh-agent
2345    0   com.mowglii.ItsycalApp.7654
-   0
-   0
-   0
-   0
-   0

I learnt a lot from and Paul Annesley's post Mac OS X: Launchd is cool.

There are five locations where launchd.plist files reside:

~/Library/LaunchAgentsPer-user agents provided by the user.
/Library/LaunchAgentsPer-user agents provided by the administrator.
/Library/LaunchDaemonsSystem-wide daemons provided by the administrator.
/System/Library/LaunchAgentsPer-user agents provided by macOS.
/System/Library/LaunchDaemonsSystem-wide daemons provided by macOS.

I want to get only .plists for daemons and agents that aren't part of macOS, so:

ls -h ~/Library/LaunchAgents /Library/LaunchAgents/ /Library/LaunchDaemons/ | grep -v apple

I get:




In my case, I discovered a launch agent, which I have fictitiously renamed in the example above, that pointed to a Preference Pane that no longer existed! I think I removed it by right clicking on the icon in System Preferences. The corresponding Status reported by launchctl list was 78 probably indicating a misconfiguration error.

I recognize most items in the list, so I'm good and decided to make no changes.


On a side note, Java 7 used a rather strange auto-update mechanism via launchd, that ignores the update setting via System Preferences > Java > Update. I don't know if it applies to Java 8.

One can remove Java by just deleting files, as described in Oracle's guide: How do I uninstall Java on my Mac?. I've never tried it since I need Java, but I'm curious if the daemon and agent get left behind. The commands provided by Oracle are:

sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane
sudo rm -fr ~/Library/Application\ Support/Java