Saturday, December 13, 2008

Cron fields and crontabs

Crontab fields

Here is a typical crontab entry and the definition of cron fields:
00 03 * * * /tmp/

  1. minute
  2. hour
  3. day of the month
  4. month of the year
  5. day of the week
  6. program or command to run

An asterisk (*) in any field means run every time for this field.
Ranges (X-Y) and steps (X-Y/Z) can also be defined.

User crontabs (including the root user)

To edit a user crontab (including root):
crontab -e

To delete a crontab:
crontab -r

System crontab

The system crontab is stored in /etc/crontab. It can be changed (as root) with a text editor.

The system crontab has one extra field before the program to run, which is the user to run the command as (usually root).

Periodic jobs in OS X Tiger

The system cron jobs were replaced in Tiger with launch daemons, in /System/Library/LaunchDaemons/. There are daily, weekly, and monthly launch daemons for system maintenance. They run shell scripts in /etc/periodic/.

The system shell scripts will run user scripts on the same schedule if they exist. To take advantage of this, simply create your own local scripts called /etc/daily.local, /etc/weekly.local, or /etc/monthly.local. They are run as root.