Show Table of Contents
Scheduling a Job as
Chapter 23. Automating System Tasks
You can configure Red Hat Enterprise Linux to automatically run tasks, also known as jobs:
- regularly at specified time using cron, see Section 23.1, “Scheduling a Recurring Job Using Cron”
- asynchronously at certain days using anacron, see Section 23.2, “Scheduling a Recurring Asynchronous Job Using Anacron”
- once at a specific time using at, see Section 23.3, “Scheduling a Job to Run at a Specific Time Using at”
- once when system load average drops to a specified value using batch, see Section 23.4, “Scheduling a Job to Run on System Load Drop Using batch”
- once on the next boot, see Section 23.5, “Scheduling a Job to Run on Next Boot Using a systemd Unit File”
This chapter describes how to perform these tasks.
23.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 23.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.
23.1.1. Prerequisites for Cron Jobs
Before scheduling a
cron job:
- Install the cronie package:
~]#
yum install cronie - The
crondservice is enabled - made to start automatically at boot time - upon installation. If you disabled the service, enable it:~]#
systemctl enable crond.service - Start the
crondservice for the current session:~]#
systemctl start crond.service - (optional) Configure cron. For example, you can change:
- shell to be used when executing jobs
- the
PATHenvironment variable - mail addressee if a job sends emails.
See the crontab(5) manual page for information on configuringcron.
23.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:
- Choose:
- in which minutes of an hour to execute the job. For example, use
0,10,20,30,40,50or0/10to specify every 10 minutes of an hour. - in which hours of a day to execute the job. For example, use
17-20to specify time from 17:00 to 20:59. - in which days of a month to execute the job. For example, use
15to specify 15th day of a month. - in which months of a year to execute the job. For example, use
Jun,Jul,Augor6,7,8to 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 * - Specify the user. The job will execute as if run by this user. For example, use
root. - Specify the command to execute. For example, use
/usr/local/bin/my-script.sh - Put the above specifications into a single line:
0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * root /usr/local/bin/my-script.sh
- 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:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # 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:
- From the user's shell, run:
[bob@localhost ~]$
crontab -eThis will start editing of the user's owncrontabfile using the editor specified by theVISUALorEDITORenvironment variable. - 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/script.sh
add:0,10,20,30,40,50 17-20 15 Jun,Jul,Aug * /home/bob/bin/script.sh
- Save the file and exit the editor.
- (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/script.sh
Scheduling Hourly, Daily, Weekly, and Monthly Jobs
To schedule an hourly, daily, weekly, or monthly job:
- Put the actions you want your job to execute into a shell script.
- 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.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.