APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds










/MacOSX/cron_not_quite_dead.html Comment posting

Comments by new and anonymous visitors are moderated and must be approved before they will appear on the site. We apologize for the necessity of this procedure.

Your name: (or leave as anonymous)
Your Website (optional): 
Your Email :

(optional - use if you want to be notified of new comments, will not be displayed )

We have strong spam controls here: preview your post to be sure it will be let through.
Comments over 6K in length cannot be posted. Please send them to me in email.

For easy cut and paste, the article text and other comments are reproduced below.


Ding, dong, the Cron is dead


2008/01/04

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



Comments