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 00:29] – [Example: MySQL] 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 ===== |
- | - open Property List Editor (included in Developer Tools) | + | - review the [[#External Links]] below to get a better understanding of how launchd and launchctl work |
- | - create entries in a new file as shown in examples below | + | - open Property List Editor (included in Developer Tools) |
- | - save the file to ~/ | + | - save the file with a namespaced filename, such as com.example.MyApp.plist |
- | - add the file to launchd | + | * if you edit the file in a text editor and launchctl complains later, you can check the file for errors with< |
- | launchctl load Library/ | + | plutil -lint <path to your plist file> |
</ | </ | ||
+ | - decide how you want the job to run | ||
+ | * ~/ | ||
+ | * / | ||
+ | * / | ||
+ | - the job will start when your computer is restarted or if you log out and back in, depending on where you placed the file. To start the job now, run this:< | ||
+ | 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:< | ||
+ | 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. | ||
===== 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 ===== | ||
+ | * http:// | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
* http:// | * http:// | ||
+ | * http:// | ||
==== GUI's ==== | ==== GUI's ==== | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
- | |||