Open Source

Schedule periodic tasks with cron

Vincent Danen covers the basics of scheduling basic system and user tasks with cron, such as downloading e-mail at certain intervals and restarting services.

Periodic schedulers come in many shapes and sizes but one constant is cron, the scheduling daemon that will run programs and scripts at arbitrary times or intervals. A number of different cron implementations exist: although vixie-cron may be the most popular, there are other similar programs such as dcron, fcron, anacron, and others. Each cron implementation typically adheres to the same standards of defining cron jobs by using a configuration file called a crontab with a certain layout:

[min] [hour] [day of month] [month] [day of week] [script or command]

For instance, most Linux systems define particular system-executed cronjobs that run hourly, daily, weekly, and monthly. These are typically defined by dropping scripts or symlinks to commands in special directories: /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly.

Different distributions may have slight variations on the location and may include support for yearly-executed scripts (such as in /etc/cron.yearly). The system crontab, /etc/crontab, is what defines when the scripts in these directories run and may look like this:

01 * * * * root nice -n 19 run-parts —report /etc/cron.hourly
02 4 * * * root nice -n 19 run-parts —report /etc/cron.daily
22 4 * * 0 root nice -n 19 run-parts —report /etc/cron.weekly
42 4 1 * * root nice -n 19 run-parts —report /etc/cron.monthly

As you can see by the above, all the scripts in /etc/cron.hourly are executed one minute after each hour (12:01, 1:01, etc.), while the daily scripts are executed at 4:02 each day. The weekly scripts are run on Sunday (day 0) at 4:22 am, while the monthly scripts are executed on the first day of each month at 4:42.

Aside from system cron jobs, users may also have user-specific cron jobs, provided they are not disallowed via the /etc/cron.deny file. A user can create his own cron jobs by creating and editing his own crontab, typically by either creating a file with the cron commands inside and importing it, or using the -e switch to directly edit the crontab, such as:

$ crontab -e

This will call the defined editor (see the value of the $EDITOR environment variable) to edit a temporary file that will become the user's crontab:

$ crontab mycrontab

Using this command will set the file mycrontab as the user's new crontab. To view the contents of your crontab, use:

$ crontab -l
*/5 * * * * /bin/touch /home/joe/foo

The above shows that every five minutes, the file /home/joe/foo will be touched.

Scheduled commands are useful for periodic system tasks, such as rotating logs or restarting services. They are useful for users in that they can be used to perform various periodic tasks such as downloading e-mail at set intervals (using fetchmail, perhaps) or connecting to remote Web sites at particular times.

Get the PDF version here.

Delivered each Tuesday, TechRepublic's free Linux and Open Source newsletter provides tips, articles, and other resources to help you hone your Linux skills. Automatically sign up today!

About Vincent Danen

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks

Free Newsletters, In your Inbox