What is cron and how is it used?

Solution Verified - Updated

Environment

  • Red Hat Enterprise Linux (all versions)

Issue

  • What is cron and how do I use it?
  • What is some general information about cron?
  • How to configure crond
  • How to setup cron jobs

Resolution

  • Cron is a time-based job scheduler, it is configured it to run commands at given times or intervals.

  • Cron is controlled by a set of files called crontabs. There is the master file in /etc/crontab (which in Red Hat Enterprise Linux is setup a little different than other *nixes), along with crontab files for the users in /var/spool/cron/. In the latter directory, the files are given the same name as a user's username.

  • In Red Hat Enterprise Linux, it is easier for the sysadmin to setup cron jobs than in other distributions. The /etc/crontab file automatically executes items in several subdirectories at regular periods.

    /etc/cron.hourly
    /etc/cron.daily
    /etc/cron.weekly
    /etc/cron.monthly
  • All the sysadmin needs to do is to place a shell script or a link to an executable in one of the directories and it will automatically be run at the appropriate time.

  • Setting up a user-level crontab is somewhat different. The files in /var/spool/cron are not edited directly. Instead, a program called crontab is used to manipulate them. Depending on system security, all users, only some, or just the root user will be able to use crontab (see man crontab sections /etc/cron.allow and /etc/cron.deny for more information).

    SYNOPSIS
           crontab [ -u user ] file
           crontab [ -u user ] { -l | -r | -e }
    
    file       store the specified file as the current crontab
    -u          user the crontab file being manipulated is for
    -l          display the current crontab
    -r          remove current crontab
    -e          edit the current crontab (editor depends on system
                variables and will probably be vi unless your sysadmin
                has changed it).
  • If not familiar with the system wide default editor vi, then it is probably best to create/edit the file with a familiar editor and use the file option with the first command.

Crontab Configuration:

  • Blank lines, leading spaces, and tabs are ignored. Lines that start with a # are comments and are ignored. Comments are not allowed to be on the same line as cron commands; they will be assumed to be part of the command. Comments are not allowed on the same line as environment settings for similar reasons.
    Syntax
    Environment settings take the format of
    
         name = value
    
    (The spaces around the = are optional.)
    
    Each cron command has 5 time and date fields, followed by a user name, and if
    this is the system crontab file, it will be followed by a command.  Commands are
    executed when the time specified by the time/date fields matches the
    current time.
    
                  field          allowed values
                  -----          --------------
                  minute         0-59
                  hour           0-23
                  day of month   0-31
                  month          0-12 (or names, see below)
                  day of week    0-7 (0 or 7 is Sun, or use names)
    
    A  field  may  be an asterisk (*), which always stands for ``first to
    last''.  So used in the hour field, it means 'every hour from 00:00 to
    23:00"
    
    Example Crontab:
    
    #      r----minute
    #      |  r-----hour
    #      |  |  r------day of the month
    #      |  |  |  r------month
    #      |  |  |  |  r------day of the week
    #      |  |  |  |  |     |------ command to run ------------->
    #      |  |  |  |  |     |

    # run five minutes after midnight, every day
           5  0  *  *  *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1

    # run at 2:15pm on the first of every month -- output mailed to Paul
           15 14 1  *  *     $HOME/bin/monthly    

    # email Joe at 10PM Monday through Friday
           0  22 *  *  1-5   mail -s "It's 10pm" joe%Joe,%%Where are your kids?%

    # print out the message at 4:05 every sunday.
           5  4  *  *  sun     echo "run at 5 after 4 every sunday"

  • If this file were saved as paul.ct then this can be used to store the crontab for the user paul.
    crontab -u paul paul.ct
SBR
Components
Category
Tags

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.