Using Cron and setting up crontabs

The service "Cron" is a utility which allows users to setup a schedule to run applications. Red hat comes with a crontab graphical tool but this document only covers the command line setup.

Cron controlled by a set of files called "crontabs". There is the master file in /etc/crontab, 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 login ID.

Using Red Hat Cron Defaults

For sys-admins, there are default locations to put scripts which run hourly, daily, weekly, or monthly. located under the following folders :

  • /etc/cron.hourly

In Red Hat, the /etc/crontab file automatically executes items in several subdirectories at regular periods. In short, scripts, applications, etc. can be easily pointed to or put in these folders and will run on the directory's scheduled pattern (i.e. daily obviously infers once per day).

Setting up crontabs

  1. For user level scheduling, the crontab -e command allows users to setup a crontab. Other basic commands are also listed.
    Basic crontab commands Description
    export EDITOR=vi
    • specify a editor to open crontab file.
    crontab -e
    • Edit your crontab file, or create one if it doesn't already exist.
    crontab -l
    • Display your crontab file.
    crontab -r
    • Remove your crontab file.
    crontab -v
    • Display the last time you edited your crontab file.

    Issue the crontab -e command to start editing your crontab (or create a new one).

    crontab -e

  2. Setting up crontab takes some getting used to, basically users will need to setup a schedule using the rules for crontab. Each crontab entry has 5 settings plus the command or script itself.
    Crontab overview:              
    | r-----hour
    | | r------day of the month
    | | | r------month
    | | | | r------day of the week
    | | | | | |------ command to run ------------->
    | | | | | |
    => * * * * *

  3. As stated already, 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 types Data values for type
    minute 0-59
    hour 0-23
    day of month 0-31
    month 0-12 (or use the names)
    day of week 0-7 (0 or 7 is Sun, or use names)

    Note: A field may be an asterisk, which always stands for 'first to last'. For example used in the hour field, it means 'every hour from 00:00 to 23:00'.
    Examples of crontab:

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

    # from 1-2 pm on Tues, Thurs, and Friday, record WAMC Public Radio
    55 12 * * 2,4,5 /home/user/apps/icecream/grab_wamc


Post a Comment