Actually, cron is not dead on Leopard; you can still use it if you like. For example, at a Terminal shell, type "crontab -e" and then a lower case "i" and then:
* * * * * /bin/date >> /tmp/dtime
Hit Escape, type ":wq" and you've created a simple cron job.
If you have no idea what you just did, see Vi Primer.
Now, every minute of every day, cron will run /bin/date and add a line to /tmp/dtime. You can confirm that's really happening with "tail -f /tmp/dtime". When you are ready to stop doing that, type "crontab -r" (don't do that if you saw other lines already in your crontab).
But your Mac OS X Leopard does not use cron. It uses /usr/sbin/periodic which comes to us by way of Leopard's BSD roots. BSD uses cron to run "periodic", but Leopard uses "Launchdaemon". You can see the files in /System/Library/Launchdaemons; there are many, but only three we care about in this context:
-rw-r--r-- 1 root wheel 579 Sep 28 23:39 com.apple.periodic-daily.plist -rw-r--r-- 1 root wheel 623 Sep 28 23:39 com.apple.periodic-monthly.plist -rw-r--r-- 1 root wheel 625 Sep 28 23:39 com.apple.periodic-weekly.plist
By the way, "launchd" does a lot more in OS X. Since "Tiger", it is PID 1, responsible for all processes. It also replaces inetd (or xinetd): it's a pretty important process!
If you want to change the time that periodic runs, you need to edit these files.. or perhaps even add new files if you have more complicated needs. Note that if you do change the times here, or add new files, you have to let launchctl know. One way is to reboot, but you can also just unload and load the file:
sudo launchctl unload -w /System/Library/Launchdaemons/com.apple.periodic-daily.plist sudo launchctl load -w /System/Library/Launchdaemons/com.apple.periodic-daily.plist
But to add new tasks for /usr/sbin/periodic, you don't need to mess around down here at all: just look in /etc/periodic. Here is somewhere where you can add your own scripts or modify the ones that are already there. For example, I wanted to automatically remove some backup files after they are a few days old. I could add my files directory to one the "clean" scripts in /etc/periodic/daily, but since those could get overwritten by an OS upgrade, it's not really the right place..
Instead, you want a local directory. It could be anywhere, but Leopard is ready to use /usr/local/etc/periodic once you create it. So I did this:
sudo mkdir -p /usr/local/etc/periodic/daily
sudo vi /usr/local/etc/periodic/daily/100mine
(added this text to the file)
#!/bin/sh
cd /Users/apl/Desktop/Shared || exit 0
/usr/bin/find . -ctime +2 -exec rm {} \;
/bin/date >> 100mine.log
(and then)
sudo chmod 755 /usr/local/etc/periodic/daily/100mine
I had previously set the com.apple.periodic-daily.plist to run at 15:45, and indeed periodic ran my file at that time:
$ ls -lut total 8 -rwxr-xr-x 1 root wheel 115 Jan 4 15:45 100mine $ ls -lut /Users/apl/Desktop/Shared/100mine.log -rw-r--r-- 1 root apl 145 Jan 4 15:45 /Users/apl/Desktop/Shared/100mine.log $ cat /Users/apl/Desktop/Shared/100mine.log Fri Jan 4 15:45:01 EST 2008
How did periodic know to look in this directory? In /etc/defaults/periodic.conf, you'll find this section:
# periodic script dirs local_periodic="/usr/local/etc/periodic /usr/X11R6/etc/periodic"
That file also defines /etc/weekly.local and /etc/monthly.local but oddly, not /etc/daily.local - I have no idea why..
As a grumpy old Unix guy, I'd probably just use crontab, but who knows: someday Apple may take that away from us entirely. That's OK, we're ready for 'em, aren't we?
Note on OS X server: Fix cron for LDAP Users in Mac OS X Server 10.5
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar