Using launchd
Now that we have Tiger, launchd seems to be the
preferred way to run regular tasks instead of cron and besides just editing the
crontab file with a new task like I used to do does not seem to work, so rather
than mucking around with it I decided to learn how to use launchd. Setting up a
launchd task to run daily is pretty straighforward....
Some good articles on
launchd:http://www.macdevcenter.com/pub/a/mac/2005/11/15/terminal5.html?page=1
http://developer.apple.com/macosx/launchd.html
Also look at the man
pagesUnderstanding the plist configuration
parameters$ man
launchd.plistUsing
launchctl$ man
launchctlI wanted to have root
user run a shell script at
/etc/xmysql-master-daily.sh
every night at 1am and append the output of the script to
/var/log/mysql/daily.log.To
implement, create and edit the plist file first, preferably in your home folder.
You can use Property List Editor.app in /Developer/Applications/Utilities or
simply use nano or pico. Also, you cna make your life easier by making a copy of
an existing launchd config file. I copied
/System/Library/LaunchDaemons/com.apple.periodic-daily.plist and used it as a
starting point in pico.The
launchd Configuration File for the
TaskHere is the launchd plist file named
mysql-master-daily.plist:<?xml
version="1.0"
encoding="UTF-8"?><!DOCTYPE
plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist
version="1.0"><dict>
<key>Label</key>
<string>mysql-master-daily</string>
<key>ProgramArguments</key>
<array>
<string>/etc/xmysql-master-daily.sh</string>
</array>
<key>UserName</key>
<string>root</string>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>1</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
<key>StandardOutPath</key>
<string>/var/log/mysql/daily.log</string></dict></plist>Where
to put the file?I was logged in as root
when working on the command line for this exercise, so you may need to precede
some commands with sudo or just switch to root with su
root.Put it in
/Library/Daemons$ cp
mysql-master-daily.plist
/Library/LaunchDaemons/To see
currently loaded launchd jobs# launchctl
listcom.apple.dashboard.advisory.fetchcom.apple.KernelEventAgentcom.apple.mDNSRespondercom.apple.nibinddcom.apple.periodic-dailycom.apple.periodic-monthlycom.apple.periodic-weeklycom.apple.portmapcom.apple.syslogdcom.vix.cronorg.postfix.masterorg.xinetd.xinetdcom.openssh.sshd#
cd /Library/LaunchDaemons/# ls
-altotal
8drwxr-xr-x 3 root wheel 102
Nov 6 11:25 .drwxrwxr-t 42 root
admin 1428 Oct 18 17:18 ..-rw-r--r--
1 root wheel 599 Nov 6 11:25
mysql-master-daily.plistLoad your
launchd job and you are done# launchctl
load mysql-master-daily.plist #
launchctl
listcom.apple.dashboard.advisory.fetchcom.apple.KernelEventAgentcom.apple.mDNSRespondercom.apple.nibinddcom.apple.periodic-dailycom.apple.periodic-monthlycom.apple.periodic-weeklycom.apple.portmapcom.apple.syslogdcom.vix.cronorg.postfix.masterorg.xinetd.xinetdcom.openssh.sshdmysql-master-dailyIf
you are on 10.4.7 or 10.4.8, you may see an error "Workaround Bonjour: Unknown
error: 0" pop into the console a few seconds after using launchctl. If so, don't
worry about it ... it's a bug that does not seem to have a detrimental effect on
anything. Google it for more info.
Posted: Monday - November 06, 2006 at 11:49 AM