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 walkdogs.sh as in the next figure.
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.
walkdogs.sh shell script
3. Save it with
Y for yes and then type in the name of the script (
walkdogs.sh) and hit enter to write the file. Change permissions with
chmod +x walkdogs.sh
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. Create user-specific
5. Now copy the
com.apple.periodic-daily.plist file from
/System/Library/LaunchDaemons to this new directory (no stinking GUI!) then check that it's there:
~/Library/LaunchAgents the directory and then do
ls to see the file listed:
cp /System/Library/LaunchDaemons/com.apple.periodic-daily.plist /Users/norburym/Library/LaunchAgents/walkdogs.plisttiger12:~ norburym$
cd ~/Library/LaunchAgentstiger12:~/Library/LaunchAgents norburym$
lswalkdogs.plist 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. Open
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. Expanded
8. The Label key is the
launchd identifier so let's name this file
walkdogs. Double-click the field with
com.apple.periodic-daily and it will become editable. Type in
9. Under Root, delete the LowPriorityIO key and the Nice key.
10. Change the ProgramArguments string field to the full path location to our
walkdogs.sh 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. Finished
12. Save the file. From the terminal,
cat the file to see what it looks like.
13. Let's load the job into
launchd's list of jobs with
launchctl load /Users/norburym/Library/LaunchAgents/walkdogs.plisttiger12:~ norburym$
14. You can check this by running
launchctl list and you will see your new
launchctl listwalkdogs tiger12:~ norburym$
15. To test this, run the
launchctl start command:
launchctl start walkdogstiger12:~ 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)."
launchd boots the system and launches root-controlled daemons like
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.
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
cron still not working properly
2007-07-18 13:04:31 email@example.com [View]
Creating launchd items for DarwinPorts items
2005-11-22 10:01:33 mathie [View]
error in text
2005-11-21 16:09:53 akakie [View]
Is StartCalendarInterval still broken in 10.4.3?
2005-11-16 21:08:33 germ65 [View]
2005-11-16 20:29:40 sjk [View]