Launchd - http://www.launchd.info/

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>

Commands