oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

An Introduction to Tiger Terminal, Part 5
Pages: 1, 2, 3, 4, 5

Roll Your Own

You now have enough information to create a launchd job. Our launchd example will run a simple shell script (review Part 4) that tells us to take a break from all this scripting and walk the dogs.

1. First, create the shell script. cd to your ~/Documents/scripts folder and nano as in the next figure.

figure 23
Figure 23. Create walkdogs shell script

2. Type in the following script (substitute the names of your own dog(s), or kids, for that matter!). Crank up the volume on your Mac and experiment with some of the other voices in the Speech pref pane, under the Text to Speech tab.

figure 24
Figure 24. shell script

3. Save it with ^X, Y for yes and then type in the name of the script ( and hit enter to write the file. Change permissions with chmod!

figure 25
Figure 25. chmod +x

Now we can write a plist file using Property List Editor or Launchd Editor or we can open another plist file that already exists and modify it for our own use. Hmmmm . . . decisions, decisions. Let's look at an existing plist file and then use that as our template in Property List Editor (since it's installed free with Apple Developers Tools; but don't discount Launchd Editor, it's great).

You'll need to create a user LaunchAgents directory (our job only affects the user, not the entire system, so it will live in ~/Library/LaunchAgents). It's very cool that launchd can run on a per user basis (to replace user login items) but the directory is not there by default so you'll need to create it.

figure 26
Figure 26. Create user-specific LaunchAgent directory

5. Now copy the file from /System/Library/LaunchDaemons to this new directory (no stinking GUI!) then check that it's there: cd to ~/Library/LaunchAgents the directory and then do ls to see the file listed:

tiger12:~ norburym$ cp /System/Library/LaunchDaemons/ /Users/norburym/Library/LaunchAgents/walkdogs.plist
tiger12:~ norburym$ cd ~/Library/LaunchAgents
tiger12:~/Library/LaunchAgents norburym$ ls
tiger12:~/Library/LaunchAgents norburym$

6. Open your new walkdogs file by double-clicking it. It will open in Property List Editor by default. Remember that this is actually the cron plist file.

figure 27
Figure 27. Open walkdogs/periodic-daily plist file

7. We'll make a few changes to it, save it and let it run. First, expand the arrow in front of Root.

figure 28
Figure 28. Expanded

8. The Label key is the launchd identifier so let's name this file walkdogs. Double-click the field with and it will become editable. Type in walkdogs.

9. Under Root, delete the LowPriorityIO key and the Nice key.

10. Change the ProgramArguments string field to the full path location to our script. Delete the key labeled "1"

11. Delete the StartCalendarInterval (StartCalendarInterval is buggy: it works once and then not again so we'll avoid it in this exercise). The minimum you need to create a plist file is the Label key and the ProgramArguments key.

figure 29
Figure 29. Finished walkdogs plist file

12. Save the file. From the terminal, cat the file to see what it looks like.

figure 30
Figure 30. cat ~/Library/LaunchAgents/walkdogs.plist

13. Let's load the job into launchd's list of jobs with launchctl:

tiger12:~ norburym$ launchctl load /Users/norburym/Library/LaunchAgents/walkdogs.plist
tiger12:~ norburym$

14. You can check this by running launchctl list and you will see your new walkdogs plist:

tiger12:~ norburym$ launchctl list
tiger12:~ norburym$

15. To test this, run the launchctl start command:

tiger12:~ norburym$ launchctl start walkdogs
tiger12:~ norburym$

Notice that I don't have to be in the directory where the file is located to start it. Volume cranked up? You should hear "Hey! Blue and Titus need a walk! It's (the current time)."

Final Thoughts

launchd boots the system and launches root-controlled daemons like xinetd, ssh, ftp, etc. and it can also launch user-specific agents, like our walkdogs example above. launchd now gives us a unified service management in OS X.

Although launchd provides similar capabilities to cron, it doesn't allow for the same level of control over commands and scripts that cron does. At least not right now! Plus, there are still some bugs like StartCalendarInterval working one time only. So cron is still required for some jobs for the time being. But with time, launchd will grow more robust, and backwards compatibility will no longer be necessary.

Mary Norbury-Glaser is the IT director at a University of Colorado affiliate center. She has over 15 years of experience in cross-platform systems administration in the education sector. She loves fast cars and geocaching.

Return to the Mac DevCenter