From Fedora Project Wiki

< Administration Guide Draft

Revision as of 16:26, 24 May 2008 by Ravidiip (talk | contribs) (1 revision(s))

Scheduling Tasks

Introduction

Fedora provides a cron facility for tasks scheduling. The cron facility is most commonly used for routine system maintenance and other recurring tasks, such as execution of backup scripts at pre-determined intervals. The cron utility runs as a crond daemon and executes tasks specified in the system or master crontab file - /etc/crontab or users' crontab files which, once created, are installed in the /var/spool/cron directory.

System crontab File

The master crontab file consists of seven fields, in the following order:

Field Meaning Valid value
1 Minute 0-59
2 Hour 0-23
3 Day of Month 1-31
4 Month 1-12 or jan,feb,mar,etc.
5 Day of Week 0-7 or sun,mon,tue,etc; where 0 and 7 = Sunday, 1 = Monday...
6 username to run the task as Any valid userID on the system
7 Task to run Any program found in the PATH which user in the Field 6 has the right to execute

Idea.png If used, the * symbol in the first five fields represents all valid values for the field.

As specified in the Fedora's default /etc/crontab file:

cat /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

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

...cron executes run-parts utility which processes all jobs found in /etc/cron.weekly directory, on every Sunday at 04:22.

The top section of the /etc/crontab file is optional and contains the lines which will set the environment variables for the jobs specified in the bottom section of the file. If these variables are not specified, the environment of the user in fieled 6 is used.

To add additional jobs to the system cron, place them in the /etc/cron.d directory.

User crontab Files

Use the crontab utility to edit, install, remove or view individual user's task schedules.

crontab Usage

Usage:

crontab <filename>

In the example above, the content of the file <filename> must conform to the crontab syntax. The only difference from the system crontab file is that the user crontab files do not contain user field. If the <filename> conforms to the crontab syntax, the crontab file of the user who runs the command will be installed in /var/spool/cron directory, with the user's name as a file name, i.e., if user root runs the command, the crontab file is created as /var/spool/cron/root.

User crontab files are not directly accessible to users, since the /var/spool/cron directory has 700 permissions set. To edit one's own crontab file the user must run the crontab command with an -e option:

crontab -e

The crontab -e may also be used to create the new crontab file. When executed, crontab -e opens the existing or new crontab file in the vim text editor. The altered crontab file is installed once it is written using :w option in vim.

To read the content of the crontab file, run:

crontab -l

To remove the crontab file, run:

crontab -r

To read or manipulate the other user's crontab file, as root run:

crontab -u <username> [options] 

To control which users can run crontab, use the /etc/cron.allow and /etc/cron.deny files. The rules are:

  • If the file /etc/cron.allow exists, add to it the username of the user to grant the access
  • If the file /etc/cron.deny exists, it must not contain the username of the user who needs access to the cron
  • If neither the /etc/cron.allow nor /etc/cron.deny exists, only the root user has access to the cron

Anacron

The anacron utility can be used to periodically schedule jobs on the machines which do not run 24 hours per day. The anacron is controlled by the /etc/anacrontab file. The /etc/anacrontab file has a syntax similar to the /etc/crontab. It consists of two sections:

  • Environment assignments section - where environment variables such as SHELL, PATH or MAILTO for the scheduled jobs are set
  • Job description section - where the actual jobs are scheduled

The job description section contains one or more lines, each consisting of four fields, separated by one or more blank space. The four fields are:

Field Meaning Valid value
1 Period n number of days
2 Delay n number of minutes
3 Job Identifier Any non-blank character, except /
4 Command to run Any shell command

The default /etc/anacrontab file on Fedora:

cat /etc/anacrontab


SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

1        65     cron.daily              nice run-parts /etc/cron.daily
7        70     cron.weekly             nice run-parts /etc/cron.weekly
@monthly 75     cron.monthly            nice run-parts /etc/cron.monthly

...executes the run-parts on all jobs from /etc/cron.weekly directory if it has not run in the last 7 days. run-parts will run with the delay of 70 minutes upon the start of the anacron daemon and use cron.weekly as an identifier for this job in anacron messages.

Additional Information

Related Manuals

Find more information about cron and anacron from the following man pages:

  • cron(8)
  • crontab(1)
  • crontab(5)
  • anacron(8)
  • anacrontab(5)