Cron is a task scheduler for Unix-based systems including all Linux distros. The cron daemon works in the background on your host and runs scheduled tasks according to the schedule. In this article, we will show how to install cron on a server running CentOS or RHEL Linux, learn the cron syntax, and schedule cron jobs with crontab.
How to Install Cron on Centos or RHEL Linux?
# dnf update -y — to update all packages on a host
# dnf install crontabs -y — to install cron
Enable the crond daemon and run it after the installation:
# systemctl enable crond.service
# systemctl start crond.service
How to Add a Cron Jobs with Crontab?
You can use the following command to add tasks to cron:
# crontab -e
This command will open a task file for your user in a default text editor (it is vim in my case, but you can change it to the one that is more convenient to you, for example, nano). This method to configure tasks prevents syntax errors. Crontab doesn’t allow saving a config file containing errors.
You can also edit the cron jobs file manually in mc:
# mcedit /var/spool/cron/root – a file name may be different depending on the user.
To add a simple job that runs a bash script in cron, enter this command:
# crontab -e
Then add the task schedule and the path to the script file:
* * * * * /root/test.sh
Save the file (it is similar to editing in vim: press Ctrl+O to save a file and Ctrl+X to exit).
If you have done it correctly, your task will be added to cron. To display the list of cron jobs, run the following command:
# cat /var/spool/cron/root
* * * * * /root/test.sh
Or this one:
# crontab -l
This script will run through cron every minute.
/etc/crontab /etc/cron.*/. /var/spool/cron/
Each crontab schedule entry consists of 5 fields:
minutes hours day_of_a_month months week_day # 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
You can use the following valid values for each of the fields:
|Day of a month||
* character means all allowed values. Here is a sample task:
30 00 * * 1 /root/test.sh
The script in the task will be run every Monday at 00:30 AM.
To make the cron file syntax easier, some special characters are used:
- A comma (,) is used to separate schedule values to run the same task at different times. For example, if you want to run a task at the 15th and 30th minute of every hour, you can set the schedule as follows:
15 * * * * 30 * * * *
Or use a shorter syntax with the comma:
15,30 * * * *
- A slash (/) is used to repeat a task. For example, you want to run a task every 2 hours. Using / you will make the contents of a cron file much shorter, otherwise, it is quite lengthy:
* */2 * * *
- A hyphen (-) indicates the range of values in a field. If you want to run a task for the first or last 10 minutes of an hour, specify the range using a hyphen:
0-10 * * * * 50-60 * * * *
Here are some more examples of cron schedules:
- to run on weekdays at 12:00 PM and at 06:00 PM:
0 12,18 * * 1-5
- every 30 minutes:
*/30 * * * *
- each Saturday:
0 0 * * 6
- every Tuesday and Thursday at 02:00 AM:
0 2 * * 2,4
You can also use special variables in cron.
||Runs once at boot|
|Once a year||
||Once a month||
||Once a week||
It means that to run a task every day, you can use the following cron syntax:
@daily echo "Cron check"
You can edit a crontab file of another user:
# crontab -u username
How to Send Cron Notifications to Email?
If you want to receive information about running your crontab tasks by email, you need to configure the cron file with the job.
# dnf install sendmail -y
# service sendmail start
Let’s configure the parameters to send emails in the cron file. Add the following lines to the file:
MAILTO="email@example.com" SHELL=/bin/bash HOME=/ * * * * * echo "Cron check"
SHELL — a user shell
HOME — a path to the cron file
Every time a cron job starts, an email notification will be sent to your mailbox.
You can save the information about running a cron task to a log file. To do it, add
>> to the end of the file and enter a path to your log file:
* * * * * echo "Cron check" >> /var/log/admin/journal.log
If there are many jobs in your crontab file, and you don’t want to get the results of some of them by email, you can run these jobs in a silent mode:
* * * * * echo "Cron check" >> /dev/null 2>&1
Cron Configuration Files & Logs
The main cron configuration file is /etc/crontab. Besides the cron file, you can run jobs from the following directories:
- /etc/cron.daily – to start scripts once a day
- /etc/cron.hourly – …. once an hour
- /etc/cron.monthly – …. once a month
- /etc/cron.weekly – …. once a week
Just put a script file in one of the directories to run it according to the schedule.
You can restrict access to the scheduler using /etc/cron.allow and /etc/cron.deny. It is enough to create these files and add users to them, who are allowed or denied to run cron tasks.
You can add jobs to the /etc/crontab as well. Usually, the file is used by the root user or to configure system tasks. Personal user files of cron jobs are stored in the /var/spool/cron/ or /var/cron/tabs/.
To track cron jobs or errors, you can view the log file: /var/log/cron. This file records all tasks and errors in the daemon operation if any: