This is an old revision of the document!
launchd
Background
Mac OS X provides two methods for launching daemons: startup items and launchd(8) daemons. Which one you use depends largely on the versions of Mac OS X that the daemon must support.
- Mac OS X v10.3 and earlier: You must use startup items. The launchd service is not supported prior to v10.4.
- Mac OS X v10.4 and earlier: You can either use startup items or launchd daemons. Using launchd daemons is preferred unless you also require backwards compatibility with versions of Mac OS X prior to v10.4.
Basic Instructions
- review the External Links below to get a better understanding of how launchd and launchctl work
- open Property List Editor (included in Developer Tools) or use a text editor to create files like the examples below
- save the file with a namespaced filename, such as com.example.MyApp.plist
- if you edit the file in a text editor and launchctl complains later, you can check the file for errors with
plutil -lint <path to your plist file>
- if you want the job to run for all users, place it in /Library/LaunchAgents/, with permissions as 644, root:wheel
- if you want the job to run only for you, place it in ~/Library/LaunchAgents/
- the job will start when your computer is restarted; to start the job now, run this:
launchctl load <path to your plist file>
- to stop the job, run this:
launchctl unload <path to your plist file>
- you can start an interactive session by running launchctl. If you do this with sudo, it will be the root launchd session; otherwise it will only be for your user
Example: Apache2
local.Apache2.plist
- Root dictionary
- Label (string): Apache2
- UserName (string): _www
- Program (string): /usr/local/apache2/bin/apachectl
- ProgramArguments (array):
- (string): start
- OnDemand (boolean): false