Chapter 24. Automating System Tasks

You can configure Red Hat Enterprise Linux to automatically run tasks, also known as jobs:
This chapter describes how to perform these tasks.

24.1. Scheduling a Recurring Job Using Cron

Cron is a service that enables you to schedule running a task, often called a job, at regular times. A cron job is only executed if the system is running on the scheduled time. For scheduling jobs that can postpone their execution to when the system boots up, so a job is not "lost" if the system is not running, see Section 24.3, “Scheduling a Job to Run at a Specific Time Using at”.
Users specify cron jobs in cron table files, also called crontab files. These files are then read by the crond service, which executes the jobs.

24.1.1. Prerequisites for Cron Jobs

Before scheduling a cron job:
  1. Install the cronie package:
    ~]# yum install cronie
  2. The crond service is enabled - made to start automatically at boot time - upon installation. If you disabled the service, enable it:
    ~]# systemctl enable crond.service
  3. Start the crond service for the current session:
    ~]# systemctl start crond.service
  4. (optional) Configure cron. For example, you can change:
    • shell to be used when executing jobs
    • the PATH environment variable
    • mail addressee if a job sends emails.
    See the crontab(5) manual page for information on configuring cron.

24.1.2. Scheduling a Cron Job

Scheduling a Job as root User

The root user uses the cron table in /etc/crontab, or, preferably, creates a cron table file in /etc/cron.d/. Use this procedure to schedule a job as root:
  1. Choose:
    • in which minutes of an hour to execute the job. For example, use 0,10,20,30,40,50 or 0/10 to specify every 10 minutes of an hour.
    • in which hours of a day to execute the job. For example, use 17-20 to specify time from 17:00 to 20:59.
    • in which days of a month to execute the job. For example, use 15 to specify 15th day of a month.
    • in which months of a year to execute the job. For example, use Jun,Jul,Aug or 6,7,8 to specify the summer months of the year.
    • in which days of the week to execute the job. For example, use * for the job to execute independently of the day of week.
    Combine the chosen values into the time specification. The above example values result into this specification:
    0,10,20,30,40,50 17-20 15 Jun,Jul,Aug *
  2. Specify the user. The job will execute as if run by this user. For example, use root.
  3. Specify the command to execute. For example, use /usr/local/bin/
  4. Put the above specifications into a single line:
    0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * root /usr/local/bin/
  5. Add the resulting line to /etc/crontab, or, preferably, create a cron table file in /etc/cron.d/ and add the line there.
The job will now run as scheduled.
For full reference on how to specify a job, see the crontab(5) manual page. For basic information, see the beginning of the /etc/crontab file:

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

Scheduling a Job as Non-root User

Non-root users can use the crontab utility to configure cron jobs. The jobs will run as if executed by that user.
To create a cron job as a specific user:
  1. From the user's shell, run:
    [bob@localhost ~]$ crontab -e
    This will start editing of the user's own crontab file using the editor specified by the VISUAL or EDITOR environment variable.
  2. Specify the job in the same way as in Scheduling a cron Job as root user, but leave out the field with user name. For example, instead of adding
    0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * bob /home/bob/bin/
    0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * /home/bob/bin/
  3. Save the file and exit the editor.
  4. (optional) To verify the new job, list the contents of the current user's crontab file by running:
    [bob@localhost ~]$ crontab -l
    @daily /home/bob/bin/

Scheduling Hourly, Daily, Weekly, and Monthly Jobs

To schedule an hourly, daily, weekly, or monthly job:
  1. Put the actions you want your job to execute into a shell script.
  2. Put the shell script into one of the following directories:
    • /etc/cron.hourly/
    • /etc/cron.daily/
    • /etc/cron.weekly/
    • /etc/cron.monthly/
From now, your script will be executed - the crond service automatically executes any scripts present in /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly directories at their corresponding times.