Launchd - http://www.launchd.info/
- is a unified, open-source service management framework for starting, stopping and managing daemons, applications, processes, and scripts
- for other UNIX Service Management Standards
Daemons & Agents
both daemons and agents are programs running in the background without requiring user input
- agent runs on behalf of the logged in user
- daemon runs on behalf of the root user or any user you specify with the UserName key
The behavior of a daemon/agent is specified in a special XML file called a property list (plist). Depending on where it is stored it will be treated as a daemon or an agent.
|
Type |
Location |
Run on Behalf of |
When Jobs Loaded |
Description |
|---|---|---|---|---|
|
User Agents |
/Users/…/Library/LaunchAgents ~/Library/LaunchAgents |
currently logged in user |
specific user login |
Job definitions for a SPECIFIC user are stored below the respective user’s Library directory |
|
Global Agents |
/Library/LaunchAgents |
currently logged in user |
any user login |
Third-Party definitions which are relevant for EVERY user are stored below /Library |
|
Global Daemons |
/Library/LaunchDaemons |
root or the user specified with the key UserName |
system start | |
|
System Agents |
/System/Library/LaunchAgents |
currently logged in user |
any user login |
Job definitions crucial for the operation of the operating system are stored below /System/Library You should never need to create a daemon or agent in these directories |
|
System Daemons |
/System/Library/LaunchDaemons |
root or the user specified with the key UserName |
system start |
The following example shows a complete plist with only three keys:
- Label This key is required for every job definition. It identifies the job and has to be unique for the launchd instance. Theoretically it is possible for an agent to have the same label as a daemon, as daemons are loaded by the root launchd whereas agents are loaded by a user launchd, but it is not recommended.
- Program This key defines what to start, in this case a shell script /Users/Me/Scripts/cleanup.sh.
- RunAtLoad This is one of several optional keys specifying when the job should be run, in this case right after it has been loaded.
<?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>/Users/Me/Scripts/cleanup.sh</string>
<key>RunAtLoad</key><true/>
</dict>
</plist>detailed version
<?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>EnvironmentVariables</key> <dict> <key>PATH</key> <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string> </dict> <key>Label</key><string>com.marcuschiu.launch</string> <key>ProgramArguments</key> <array><string>/Users/marcuschiu/Desktop/launch.sh</string></array> <key>RunAtLoad</key><true/> <key>KeepAlive</key><false/> <key>LaunchOnlyOnce</key><true/> <key>StandardOutPath</key><string>/tmp/startup.stdout</string> <key>StandardErrorPath</key><string>/tmp/startup.stderr</string> <key>UserName</key><string>marcuschiu</string> <key>GroupName</key><string>staff</string> <key>InitGroups</key><true/> </dict> </plist>