BSD DevCenter
oreilly.comSafari Books Online.Conferences.


FreeBSD Basics

Getting Cron to Do Our Bidding


Wouldn't it be great if you could get your FreeBSD system to automatically perform maintenance tasks on a regular basis and e-mail you the results? Well, thanks to the built-in cron daemon and the periodic scripts, your FreeBSD system is already doing this for you. In today's article, we'll concentrate on FreeBSD's daemon to execute scheduled commands and learn how to configure the cron daemon to run your own commands and scripts.

Like most other daemons, cron is started for you when you boot into FreeBSD and continues to quietly run in the background. To prove to yourself that cron is indeed running, look for the word cron in the results of the process status command like so:

ps -ax | grep cron
   97  ??  Is     0:07.71 cron

In this example, cron is running with a process ID of 97.

The cron daemon "wakes up" every minute and checks the crontabs (short for chron or time tables) to see if it needs to start the execution of a command or script. A crontab is simply a file containing a list of commands and the time you want each command to start running. When you installed FreeBSD, a system crontab was created for you. You should not make any changes to this crontab file; later on in the article, we'll use the crontab utility to make user crontabs that cron will read in addition to the system crontab.

The system crontab is stored in the /etc directory. We'll use the more command to safely view this file without risk of editing it. You'll want to be logged in to two virtual terminals as we'll be reading the system crontab file at one terminal, and trying other commands at the other terminal. At the first virtual terminal, type:

more /etc/crontab

which should result in this output:

# /etc/crontab - root's crontab for FreeBSD
# $FreeBSD: src/etc/crontab,v 1.21 1999/12/15 
17:58:29 obrien Exp $
#minute	hour	mday	month	wday	who	command
*/5	*	*	*	*	root	/usr/libexec/atrun

As in any file, remarks begin with a #. The most useful remark is the labeling of the fields: Note that there are five time fields, a user field, and a command field. While the syntax may seem strange at first, each uncommented line in the crontab simply tells cron when you want it to run a specified command.

The following table shows the valid values for the five time fields:


Valid Values








1-12 or first three letters of each month (not case-sensitive)


0-7 (where both 0 and 7 represent Sunday) or first three letters of each day (not case-sensitive)

Values can be a single number or three-letter word, or values can be a range: For example, 1-5 in the dayofweek field represents Monday to Friday. Values can also be separated by a comma: 1,15,30 in the dayofmonth field will run a command on the 1st, 15th, and 30th of each month.

All five time fields accept the * wildcard, which means "use every valid value" for that field. For example, to run a command every month, put a * in the month field.

You can also use the */number value. For example a */2 in the month field means "run every second month." The system crontab has an example of this:

#minute	hour	mday	month	wday	who	command
*/5	*	*	*	*	root	/usr/libexec/atrun

This line is read as: Every minute that is divisible by five (or every five minutes) of every hour of every day of every month, run the atrun command as root. If you don't know what the atrun command does, type this at your other virtual terminal:

whatis atrun
atrun(8) - run jobs queued for later execution

And if that doesn't satisfy your curiosity:

man 8 atrun

Pages: 1, 2, 3

Next Pagearrow

Sponsored by: