Many tasks that a computer does do not require user interaction and are most effectively handled by another process that runs in the background. This orchestry can be done using the init system. The init system is responsible for launching services, shutting them down again and to restart them if they fail. 1 2
Launchd is the init system that the macOS platform utilizes. It manages processes, both system-wide and for individual users and can be controlled using the launchctl cli. Unlike other init systems, where every process that is controlled by the init system is called a daemon, only launchd’s system-wide managed processes are called daemon. Programs that should run in the background as a user are called agent. Additionally launchd also manages XPC services that are bundled within applications and frameworks on the system. XPC services which are marked with a ServiceType of System are functionally equivalent to daemons and run in the same environment, sharing the same Mach bootstrap namespace. XPC services that are marked with the ServiceType of User are equivalent to agents and run in the same environment, sharing the same Mach bootstrap namespace.
The definitions for the different daemons and agents are:
~/Library/LaunchAgents- Per-user agents provided by the user/Library/LaunchAgents- Per-user agents provided by the administrator/Library/LaunchDaemons- System-wide daemons provided by the administrator/System/Library/LaunchAgents- Per-user agents provided by Apple/System/Library/LaunchDaemons- System-wide agents provided by Apple
launchd’s utilizes some data structures, namely domains, services and endpoints. A domain manages the execution policy for a collection of services. A service is a process that can be spawned in response to demand. An endpoint is a place where other processes can send messages to. Sending a message to an endpoint will cause launchd to start the needed service. Endpoints are in a shared namespace and may be thought of as synonymous with Mach bootstrap subsets.
For example, an audio server may be available in domain X and provides the output endpoint. This endpoint takes an integer and outputs them to the speaker. If no audio is playing, the audio server doesn’t need to be run. If however an application tries to play audio, launchd is responsible to start the audio server.
launchctl
The launchctl cli is the main interface to launchd. It allows for detailed examination of the data structures that launchd consists of. Many subcommands in launchctl take a specifier which indicates the target domain or service for the subcommand. This specifier may take one of the following forms:
system/<service-name>- Targets the system domain or a service within the system domain. Anyone may read or query the system domain, but root privileges are required to make modifications.user/<uid>/<service-name>- Targets the user domain for the given UID or a service within that domain. A user domain may exist independently of a logged-in user. User domains do not exist on iOS.login/<asid>/<service-name>- Targets a user-login domain or service within that domain. This domain is created when the user logs in at the GUI and is identified by the audit session identifier associated with that login. User-login domains do not exist on iOS.gui/<uid>/<service-name>- Another form of theloginspecifier. Rather than rspecifying a user-login domain by its ASID, this specifier targets the domain based on which user it is associated with and is generally more convenient.pid/<pid>/<service-name>- Targets the domain for a given PID or a service within that domain. Each process on the system will have a PID domain associated with it that consists of the XPC services visible to that process.
For example, when referring to a service with the identifier com.apple.example loaded into the GUI domain of the user with the UID 501, domain-target is gui/501/, service-name is com.apple.example and service-target is gui/501/com.apple.example.
NOTE: GUI domains and user domains share many resources. For the purposes of the Mach bootstrap name lookups, they are “flat”, so they share the same set of registered names. But they still have discrete sets of services. So when printing the user domain’s contents, you may see many Mach bootstrap name registrations from services that exist in the GUI domain for that user, but you will not see the services themselves in that list.
Starting a service
Starting a service will start the service immediately until a computer reboot or if the stop option is called.
NOTE: that if trying to start a daemon, root privileges are needed
$ launchctl start <service-name>
For example
Stopping a service
This will stop a service immediately. A service may be started again using the start option or, if the service is enabled, after a reboot.
NOTE: that if trying to start a daemon, root privileges are needed
$ launchctl stop <service-name>
Creating a service
A new service may be created in the form of a PLIST. A minimal service needs to include three keys and their corresponding value:
NOTE: A service file must end in the
.plistsuffix.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.example.app</string>
<key>Program</key>
<string>/usr/local/bin/example</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
This will start a service that is identified by it’s service name (Label) com.example.app. The Program key-value tells launchd what program to invoke and lastly with RunAtLoad, launchd is told when the command should be executed. In our case, /usr/local/bin/example is invoked directly at the service’s load-time. 3
The definition above corresponds to the following command:
/usr/local/bin/example
Valid keys
Launchd supports more than 36 different configuration keys in service PLIST’s. This section explains those keys in depth.
Label
The Label key is required in every service definition. It identifies the service and has to be unique.
NOTE: It is theoretically possible for agents and daemons to have the same
Label, as they are ran in different environments. This is however not recommended.
Normally, the value of Label’s are written in reverse domain notation, for example com.apple.bluetoothd, however every label is possible.
Example
<key>Label</key>
<string>com.example.app</string>
Program
The Program key tells launchd what program to launch.
Example
<key>Program</key>
<string>/path/to/your/executable</string>
This will result in the following shell code:
/path/to/your/executable
ProgramArguments
Many programs cannot be simply invoked, but rather need to have additional arguments. These can be added using the ProgramArguments key. This key will take an array of strings, which then will be added as arguments to the Program command.
NOTE: It is possible to leave out the
Programkey alltogether and just useProgramArguments. This however complicates things when reading and is not recommended.
Example
<key>ProgramArguments</key>
<array>
<string>--path</string>
<string>/Users/test/mywebsite/index.html</string>
</array>
This will result in the following shell code being appended to the Program variable:
--path /Users/test/mywebsite/index.html
Debugging serivce files
Service definitions can and will be faulty in some cases. Launchd wont start services that it doesn’t understand or cannot run. To debug service files, macOS has several built-in tools to assist.
By checking the logfile
Launchd creates and writes changes into a logfile, which is located in /var/log/system.log. It might be useful to search this logfile for errors from your service:
cat /var/log/system.log | grep <service-name>
Using plutil
The plutil can be used to lint PLIST files and show potential errors in the definition:
$ plutil -lint <path-to-PLIST>