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

Ding, dong, the Cron is dead

© May 2019 Anthony Lawrence

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)
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

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Ding, dong, the Cron is dead

Inexpensive and informative Apple related e-books:

Take Control of Preview

Digital Sharing Crash Course

Are Your Bits Flipped?

Photos for Mac: A Take Control Crash Course

Take Control of Upgrading to El Capitan

More Articles by © Anthony Lawrence

Printer Friendly Version

Have you tried Searching this site?

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.

Contact us

Printer Friendly Version

Securing a computer system has traditionally been a battle of wits: the penetrator tries to find the holes, and the designer tries to close them. (Gosser)

Linux posts

Troubleshooting posts

This post tagged:



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode

SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)