Search:  
Gentoo Wiki

Cron

Contents

Introduction

Cron is a daemon that executes scripts or programs according to schedules defined in "crontab" files. The commands are executed repeatedly at intervals that can range from minutes to months. The cron daemon checks the crontab files once each minute. The commands can be anything: automated backups, portage updates, yearly holiday emails, whatever.

If you need to schedule a command that will be executed only one time, use "at."

Installation

This article assumes vixie-cron, which is the most widely used cron daemon. Alternatives to vixie-cron are noted in a later section.

To install vixie-cron and to start the cron daemon when booting:

# emerge vixie-cron
# rc-update add vixie-cron default

This will emerge "sys-process/cronbase" also.

Crontab Files

Here is an example of a crontab file:

# /etc/crontab.cron

# All mail from cron is sent to root
MAILTO=root

#       minute (0-59),
#       |       hour (0-23),
#       |       |       day of the month (1-31),
#       |       |       |       month of the year (1-12),
#       |       |       |       |       day of the week (0-6 with 0=Sunday).
#       |       |       |       |       |       commands
# ------{ daily cron jobs }------------ #
        0       10      *       *       *       /usr/sbin/logwatch.pl
        0       8       *       *       *       updatedb
        0       4       *       *       0       /usr/bin/emerge -uD world
        0       0       *       *       0       /usr/bin/emerge --sync
        0       18      *       *       *       /usr/sbin/logrotate --verbose /etc/logrotate.conf

A field ( minute, hour, day... ) can contain several values separated by commas (but not spaces), e.g., 1,3,13. A range of values can be designated by a hyphen, e.g., 3-5.

field allowed values
minute 0-59
hour 0-23
day of the month 1-31
month 0-12 (or names)
day of the week 0-7 (0 or 7 is Sun, or names)
command A linux command

The asterisk * stands for first-last, i.e., it is always satisfied. You can use the first three letters of the day of the week or of the month instead of a number. A field value of the form */n where n is a number means use every nth value for that field, e.g., a */2 in the first field means every two minutes. See crontab(5) for a more complete discussion.

There is a "system" crontab file: /etc/crontab. Also, each user including root can have his or her own crontab file. These user crontab files are named after the user and are found in /var/spool/cron/crontab. The cron daemon checks them all each minute.

User crontab files (but not the system crontab file) can be edited using the "crontab -e" command. (Crontab is a command as well as the name of a table.) Your default editor is used. The advantage of this command is that it creates, stores, and retrieves your crontab file for you. "crontab -l" lists your crontab file. "crontab -r" deletes your crontab file. "crontab somefile" will make "somefile" your user crontab file. All crontab files including the system crontab file can also be edited using nano or another similar editor. Note that the crontab command does not provide the cron daemon; that is a separate function.

run-parts & run-crons

Two commands that are often used in crontab files are "run-parts" and "run-crons." "run-parts" executes all the executable files in a directory. For example:

# run-parts /etc/cron.daily

"run-parts" is a part of the Gentoo "sys-apps/debianutils" package.

"run-crons" executes all the scripts in the /etc/cron.daily, /etc/cron.hourly, /etc/cron.monthly, and /etc/cron.weekly directories. "run-crons" is found in the sys-process/cronbase package. The default /etc/crontab that is installed when vixie-cron is emerged has a run-crons command.

If the computer wasn't running...

A potential problem with cron packages is that the computer may not be running when it is time for a command to be executed.

Anacron is a possible solution. Anacron has its own table: /etc/anacrontab. Here is an example:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# format: period delay job-identifier command
1     5       cron.daily      run-parts /etc/cron.daily
7     10      cron.weekly     run-parts /etc/cron.weekly
30    15      cron.monthly    run-parts /etc/cron.monthly

Anacron runs a command if that command has not been run for some time. The time is specified in the first column of anacrontab and is measured in days. In the above example, anacron will run the scripts in cron.daily if they have not been run in previous 24 hours. Anacron touches files in /var/spool/anacron to record when commands were last run. The second column in anacrontab is a delay in minutes before the command is actually run. The purpose is to assure that several commands will not run simultaneously, bogging down the computer. Anacron can be run by adding it to a crontab or by adding it to /etc/runlevels/default using rc-update. In the latter case, it will run when the computer is booted.

"run-crons" also runs scripts that were skipped because the computer was shut down. In this case, the scripts must be in the /etc/cron.hourly (or daily or weekly or monthly) directories. "run-crons" touches files in /var/spool/cron/lastrun to record the times that files were last executed.

The fcron web site asserts that "...fcron does both the job of Vixie Cron and anacron, but does even more and better :)) ..."

If you want to use anacron:

# emerge anacron

and, perhaps:

# rc-update add anacron default

Security

This article does not explore security issues.

A user must be a part of the cron group to edit his or her own crontab. The following command enables root to add a user to the cron group:

# gpasswd -a username cron

If /etc/cron.deny does not exist or if /etc/cron.allow does not contain the user's name, a user other than root cannot use crontab commands.

Alternative Gentoo Cron Packages

Information about alternatives to vixie-cron can be found in the Official guide Gentoo Linux Cron Guide and at these web sites:

dcron: http://apollo.backplane.com/FreeSrc/
fcron: http://fcron.free.fr/
bcron: http://untroubled.org/bcron/

See Also

Retrieved from "http://www.gentoo-wiki.info/Cron"

Last modified: Sat, 30 Aug 2008 19:45:00 +0000 Hits: 8,272