Managing macOS devices often requires working with background processes that run at startup or in the background to support critical functions. Two key components that help with this are LaunchDaemons and LaunchAgents. While both are crucial for automating tasks, they serve distinct purposes.
What Are LaunchDaemons?
LaunchDaemons are background processes that run as root and start when the system boots. They can run tasks that don’t require user interaction, such as monitoring services or executing scheduled scripts. They are ideal for system-level services that need to persist even before or without a user logging in.
Key traits:
- Run as root (superuser), unless the plist sets a
UserName. - Start at boot time, before any user logs in.
- Do not interact with the user interface.
Typical use cases for LaunchDaemons include tasks like network monitoring, system backups, or enforcing security policies across all users.
What Are LaunchAgents?
LaunchAgents, on the other hand, are user-level processes. They start when a specific user logs in and can interact with the user interface. These processes are ideal for tasks that require user interaction or are tied to a specific user’s session, such as syncing apps, updating preferences, or triggering notifications.
Key traits:
- Run as the logged-in user.
- Start when the user logs in.
- Can interact with the user interface.
LaunchAgents are frequently used for tasks like automatic updates, app launching, and file synchronization based on user-specific preferences.
LaunchDaemons vs LaunchAgents: Key Differences
| LaunchDaemons | LaunchAgents | |
|---|---|---|
| Runs as | root (or the UserName set in the plist) |
The logged-in user |
| Starts | At boot, before anyone logs in | When a user logs in |
| User interface access | No | Yes |
| Scope | The whole Mac | One user session (each user gets their own copy) |
| Location for your own items | /Library/LaunchDaemons/ |
/Library/LaunchAgents/ (all users) or ~/Library/LaunchAgents/ (one user) |
| Apple’s own items (do not edit) | /System/Library/LaunchDaemons/ |
/System/Library/LaunchAgents/ |
| Load with | sudo launchctl bootstrap system <plist> |
launchctl bootstrap gui/<uid> <plist> |
Example: A Daily Script as a LaunchDaemon
Save this as /Library/LaunchDaemons/com.example.cleanup.plist. It runs a script every day at 02:00 as root:
<?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.cleanup</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/cleanup.sh</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>2</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
</dict>
</plist>
Check the file, set the ownership and permissions launchd requires, then load it:
plutil -lint /Library/LaunchDaemons/com.example.cleanup.plist
sudo chown root:wheel /Library/LaunchDaemons/com.example.cleanup.plist
sudo chmod 644 /Library/LaunchDaemons/com.example.cleanup.plist
sudo launchctl bootstrap system /Library/LaunchDaemons/com.example.cleanup.plist
To confirm it is loaded, and to unload it again:
sudo launchctl print system/com.example.cleanup
sudo launchctl bootout system/com.example.cleanup
For a LaunchAgent, use the user’s GUI domain instead of system, for example launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.agent.plist.
Best Practices for Managing LaunchDaemons and LaunchAgents
- Use PLIST files: Both LaunchDaemons and LaunchAgents are managed using property list (.plist) files. These files define when and how these processes run. Ensure your PLIST files are correctly configured to avoid performance or security issues.
- Test before deployment: Always test LaunchDaemons and LaunchAgents in a controlled environment to ensure they run as expected without causing system slowdowns.
- Keep security in mind: Since LaunchDaemons run as root, they can introduce security risks if improperly configured. Always follow security best practices, including limiting privileges and monitoring system behaviour.
By properly utilising LaunchDaemons and LaunchAgents, IT admins can automate tasks and maintain a smooth, efficient macOS environment.