Category Image 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 pages
Understanding the plist configuration parameters
$ man launchd.plist

Using launchctl
$ man launchctl

I 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 Task
Here 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 list
com.apple.dashboard.advisory.fetch
com.apple.KernelEventAgent
com.apple.mDNSResponder
com.apple.nibindd
com.apple.periodic-daily
com.apple.periodic-monthly
com.apple.periodic-weekly
com.apple.portmap
com.apple.syslogd
com.vix.cron
org.postfix.master
org.xinetd.xinetd
com.openssh.sshd

# cd /Library/LaunchDaemons/
# ls -al
total 8
drwxr-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.plist

Load your launchd job and you are done
# launchctl load mysql-master-daily.plist

# launchctl list
com.apple.dashboard.advisory.fetch
com.apple.KernelEventAgent
com.apple.mDNSResponder
com.apple.nibindd
com.apple.periodic-daily
com.apple.periodic-monthly
com.apple.periodic-weekly
com.apple.portmap
com.apple.syslogd
com.vix.cron
org.postfix.master
org.xinetd.xinetd
com.openssh.sshd
mysql-master-daily

If 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        


Published by