cron: Job Scheduler

by Michael S. Keller

The cron daemon, crond, packaged with most Linux distributions, controls scheduling of regularly occurring jobs. When started upon entry into multi-user mode, crond scans the directories /var/spool/cron/crontabs and /etc/cron.d and the file /etc/crontab for work to do. crond then awakens every minute, performs the work its record of jobs says it should do at that time, mails the output (by default) to the owning user, then sleeps until the beginning of the next minute.

The implementation of crond packaged with Debian 2.0, the distribution I used when writing this article, carries the name Vixie Cron, after Paul Vixie, its author. I will use “cron” to refer, variously, to both the crond process and the cron facility.

History of cron

cron evolved to enable the execution of jobs at regular intervals. Have you had occasion to use the log files in /var/log? Most Linux distributions come with a ready set of cron jobs to tame those log files. Without cron jobs, the file system holding /var would eventually fill completely with log files. The potential uses for cron exceed the small customizations I have made to my home environment. If you want to automate something that runs more than once, turn to cron.

How to Use cron

Individual users may use cron to automate tasks. Normally, all users may make use of cron. If superuser has created /etc/cron.allow or /etc/cron.deny, then access to the cron facility depends on the contents of those files. If /etc/cron.allow exists, your user name must appear in it for you to use cron. If /etc/cron.deny exists but /etc/cron.allow does not, your user name must not appear in /etc/cron.deny, or cron will not work for you. To edit your cron settings, use the crontab command:

crontab -e

This will create a cron table, or “crontab file”, which cron will read to find work. The crontab command looks first for the VISUAL environment variable, then for the EDITOR environment variable. It will use the editor named in those variables to provide editing of crontab files. Without one of these environment variables set, Debian 2.0's crontab uses the ae editor. Other distributions may have a different default behavior for crontab. Make the changes you desire, save the file and exit the editor.

Why do we not edit the crontab file directly? The reason is cron requires a specific format for its job entries. The crontab command performs syntax checking before allowing a newly edited crontab file to enter circulation. If the new crontab has a syntax error, crontab complains and asks if you want to edit again. To protect the crontab files, the crontab command makes root the owner of the crontab files.

Listing 1.

To view your newly edited crontab file, use this command:

crontab -l

The output should look something like Listing 1. Each crontab entry provides either an environment variable or a time-specific cron command. cron sets a few environment variables automatically. Others, such as MAILTO, can be set by the user. Normally, cron mails the output of each cron job to its owner. If you put the line

in your crontab file, the output of your cron jobs would go to user fred instead. More likely, you would want to suppress cron output. If you set MAILTO to null,
then cron will discard the job output.

The fields in a time-specific cron command appear in this order: minute (0-59 allowed), hour (0-23 allowed), day of month (1-31 allowed), month (1-12 or names allowed), day of week (0-7 or names allowed, with both 0 and 7 representing Sunday), and the command to run. The numerical fields also allow ranges of numbers, wild cards, lists and methods for running cron jobs at every Nth interval, such as every third hour. The asterisk character works as a wild card, representing every occurrence of the field's value. For details, see the crontab(5) manual page.

The example below will run the ls command every minute of the noon hour on the first day of the month, discarding the output:

# Minute Hour Day-Of-Month Month Day-Of-Week
# Command
* 12 1 * * /usr/bin/ls

This next example will run the free command every other hour and mail the output to fred:

* */2 * * * /usr/bin/free
The system-wide crontab, stored in the file /etc/crontab, provides a slightly different method for running cron jobs. It does not have a special editor, so you must take extra care when editing it. In addition, it provides a user name field between the Day-of-Week and Command fields, to run jobs under a user ID other than root, without having to create a separate crontab file for that user. Edit it with your favorite editor and save the changes; cron will automatically update its job list.
Pre-Configured cron Jobs

The Debian and Red Hat distributions come with several pre-configured cron jobs to help control disk usage. Other distributions may provide similar help. The Debian dh_installcron command will install these jobs. Normally, you should not need to run this command—installing the cron package will take care of it for you.

These jobs, located in the file /etc/crontab, use the run-parts command to call all the scripts in directories /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly. For the most part, these scripts control disk usage, compressing and pruning log files in /var/log and cleaning up after indices from the man(1) command. The package maintainers who created these jobs configured them to run during the night, normally a slow time for other system activity. Some of the scripts generate a lot of disk activity, which can slow other I/O-intensive jobs. If you want them to run at other times, edit /etc/crontab or move the scripts among the directories that contain them.

Additional Reading

See the list of references in the sidebar for additional reading on cron. Additionally, the man page for the at facility may prove useful. at provides a one-time job-scheduling facility. If you do not keep your Linux system running 24 hours per day, you may want to review Anacron, which does not depend on specific time events to get its work done.


I have provided a brief introduction to the cron facility, a typical part of Linux and other UNIX operating systems. It will provide a starting point for time-related work you want your Linux system to perform. In brief, if you want to schedule repetitive tasks so as not to type the same commands again and again, use cron.


Michael S. Keller works as a technical analyst with Sprint Paranet, a wholly owned subsidiary of Sprint, a nationwide network services provider based in Houston. He has used UNIX variants for nearly nine years and enjoys communing with cats, motorcycles and the universe. You may reach him at
Load Disqus comments