Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
docs:mac:launchd [2011/04/16 12:27] – billh | docs:mac:launchd [2012/07/01 15:28] (current) – billh | ||
---|---|---|---|
Line 4: | Line 4: | ||
Mac OS X provides two methods for launching daemons: [[creating startup items|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 provides two methods for launching daemons: [[creating startup items|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.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. | + | * Mac OS X v10.4 and later: 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 | + | ===== Creating a launchd job ===== |
- review the [[#External Links]] below to get a better understanding of how launchd and launchctl work | - 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 | - open Property List Editor (included in Developer Tools) or use a text editor to create files like the examples below | ||
Line 13: | Line 13: | ||
plutil -lint <path to your plist file> | plutil -lint <path to your plist file> | ||
</ | </ | ||
- | - if you want the job to run for all users, place it in / | + | - decide how you want the job to run |
- | - 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:< | + | * / |
+ | * /Library/LaunchDaemons/ - will run when system starts, as root; file permissions need to be 644, owned as root | ||
+ | - the job will start when your computer is restarted | ||
launchctl load <path to your plist file> | launchctl load <path to your plist file> | ||
+ | </ | ||
+ | * if you get a " | ||
+ | launchctl load -w <path to your plist file> | ||
</ | </ | ||
- to stop the job, run this:< | - to stop the job, run this:< | ||
launchctl unload <path to your plist file> | launchctl unload <path to your plist file> | ||
</ | </ | ||
+ | - if the job needs to run as root, use sudo before the commands | ||
- you can start an interactive session by running launchctl. | - you can start an interactive session by running launchctl. | ||
===== Example: Apache2 ===== | ===== Example: Apache2 ===== | ||
- | **local.Apache2.plist** | + | Since the apachectl program spawns a daemon and doesn' |
- | * Root dictionary | + | |
- | | + | **/usr/local/ |
- | | + | <code sh> |
- | * Program | + | #!/bin/sh |
- | | + | / |
- | * (string): start | + | |
- | | + | sleeptime=40 |
+ | |||
+ | httpdArray=(`ps -U www | grep httpd | awk ' | ||
+ | let httpdCount=${# | ||
+ | | ||
+ | while (($httpdCount > 0)) | ||
+ | do | ||
+ | | ||
+ | hpptdArray=(`ps -U www | grep httpd | awk ' | ||
+ | | ||
+ | done | ||
+ | |||
+ | echo " | ||
+ | </ | ||
+ | |||
+ | **/ | ||
+ | <code xml> | ||
+ | <?xml version=" | ||
+ | < | ||
+ | " | ||
+ | <plist version=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <key>Program</ | ||
+ | <string>/ | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | <string> | ||
+ | | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
===== Example: MySQL ===== | ===== Example: MySQL ===== | ||
- | http://hints.macworld.com/article.php?story=20080128103022907 | + | **/ |
+ | |||
+ | <code xml> | ||
+ | <?xml version=" | ||
+ | < | ||
+ | <plist version=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== Example: Postgres ===== | ||
+ | **/ | ||
+ | |||
+ | * note - the below script causes console to log this:< | ||
+ | * 7/1/12 2:24:05.272 PM com.apple.launchd: | ||
+ | </ | ||
+ | * however, if you try changing the users to _postgres, the launch fails (doesn' | ||
+ | |||
+ | <code xml> | ||
+ | < | ||
+ | " | ||
+ | <plist version=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
===== External Links ===== | ===== External Links ===== | ||
Line 42: | Line 146: | ||
* [[http:// | * [[http:// | ||
* http:// | * http:// | ||
+ | * http:// | ||
==== GUI's ==== | ==== GUI's ==== | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
- | |||