launchd and cron job
Saturday, April 30th, 2005In OS X.4, one would discover that the file /etc/crontab is empty. If you try “sudo crontab -e“, you will find the default cron table for root is also empty. How does OS X.4 control these the timely maintenance jobs? I need to run a simple job to book keeping my battery usage log every 10 minutes. I know that I can still use “cron” to achieve this goal, however, I think it would be more fun to dig out how to archive this by using OS X way, launchd.
After reading several man pages for launchd, launchctl and launchd.plist, I find it is not hard to use the new launchd to run a program periodically. Basically, each line in the crontab is replaced by an XML file (property list), to describe how a program should be launched by the launchd daemon. One can refer to Apple’s /System/Library/LaunchDaemons/com.apple.periodic-daily.plist for the general structure of the property list format. You can create your own myjob.plist and put it in /Library/LaunchDaemon or ~ /Library/LaunchDaemon, then the system will load it in automatically after reboot or re-login. Actually, I am not sure if reboot or re-login is required. Nevertheless, you can also load the job manually by “launchctl load” and “launchctl start“. If you are interested in what jobs have been loaded by launchd, try “launchctl list” or “sudo launchctl list”.
I have not studied the mechanism behind launchd, but I notice that my weekly mantainace jobs do not run at the time specified by its configuration file. Dose launchd already have some sort of anacron function? If so, it will be a great news for powerbook or ibook user.
launchd seems to be a very capable daemon to replace some of those old unix based launchers. I like the XML property list file better than the old crontab format, which I never remember the meaning of different field unless I look up the man page. With a flexible XML format, it is quite likely to have a nice gui program to manage all launchd jobs.
General Background about launchd