Setting up Cron Task Scheduler on CentOS 8

CronJob is a task scheduler in Linux that schedules a task at a specific time or schedules a task to repeat after a specific time. Here we will show you how to setup Cron Job on CentOS 8.


CronJob is a LinuxA program that schedules a task at a specific time or schedules a task to repeat after a specific time. Here is how to set it up on CentOS 8 Job.
Cron is a time-based task management system for Unix-like operating systems. Users can use cron to run regular tasks (which can be commands and scripts) at fixed times, dates, and intervals. Cron is often used for maintenance and management, but can also be used in other places, such as regularly downloading files and emails. The word cron comes from the Greek word chronos, which originally means time.
Typically, the instructions stored in the task schedule (crontab) file are activated by the crond daemon, which runs in the background and checks every minute whether there are regular jobs that need to be executed. Such jobs are generally called cron jobs.
Before you start setting up Cron jobs on CentOS 8, you must have a non-root user account with sudo privileges on your server.
To open crontab using a text editor, enter the following command:

crontab -e

After entering the above command, you will be asked to choose a text editor.
In crontab, the mh dom mon dow user header has the following meaning:
* m = The minute the cron job will run. (0 to 59) * h = An integer determining the hour the task will run. (0 to 23) * dom = The day of the month the cron job will run. (1 to 31). * mon = The day of the month the cron job will run. (1 to 12) * dow = The day of the week, from 0-6 with Sunday being 0. (0 to 6) * user = The user the cron will run under. * command = The Linux command you want to execute.
The asterisk on the crontab timing (
When setting up cronjob asterisk (
), it is widely used. This means that ifExpressed as m (minutes), it will run the command every minute.
Basic Example of Cron Jobs
Cron that runs every minute

* * * * * (user) (command)

Run Cron every 10 minutes

10 * * * * (user) (command)

Run Cron every 30 minutes

30 * * * * (user) (command)

Cron that runs every hour (when the minute reaches zero)

0 * * * * (user) (command)

Cron that runs at midnight

0 0 * * * (user) (command)

Cron that runs at 8 AM

0 8 * * * (user) (command)

Cron to run PHP script

 * * * * * root /usr/bin/php /var/www/html/project/test.php

Advanced examples of crontab
Using commas in crontab: The following command will be executed at 8:45 AM on January, March, July and December 15th.

45 8 15 Jan,Mar,Jul,Dec * (user) (command)

Use the division operator in crontab: Divide the minute by 10. The following command will be executed at the 0th, 10th, 20th, 30th, 40th, 50th minute of every hour (every minute is divisible by 10).

*/10 * * * * (user) (command)

Dashes in crontab: The following crontab will run from the 15th to the 20th of every month. The dash represents a range.

0 0 15-20 * *(user) (command)

Detailed explanation of cron expressions:
Cron expression is a string, separated by 5 or 6 spaces, divided into 6 or 7 fields, each field represents a meaning, Cron has the following two syntax formats:
Seconds Minutes Hours DayofMonth Month DayofWeek Year or
Seconds Minutes Hours DayofMonth Month DayofWeek
The characters that can appear in each field are as follows:
Seconds: can contain ", - * /" four characters, valid range is 0-59 integer
Minutes: can contain ", - * /" four characters, valid range is integer 0-59
Hours: can contain ", - * /" four characters, valid range is 0-23 integer
DayofMonth: can contain ", - * / ? LWC" eight characters, valid range is integer 0-31
Month: ", - * /" four characters are allowed, the valid range is an integer from 1 to 12 or JAN-DEC
DayofWeek: ", - * / ? LC #" can appear. The valid range is an integer from 1 to 7 or two ranges from SUN to SAT. 1 means Sunday, 2 means Monday, and so on.
Year: can contain ", - * /" four characters, the valid range is 1970-2099
Each field uses numbers, but the following special characters may also appear, and their meanings are:
*: Indicates that any value in the field is matched. If used in the Minutes field, it means that the event will be triggered every minute.
?: can only be used in the DayofMonth and DayofWeek fields. It can also match any value in the field, but it actually does not. This is because DayofMonth and DayofWeek affect each other. For example, if you want to trigger a schedule on the 20th of each month, regardless of what day of the week the 20th is, you can only use the following syntax: 13 13 15 20 * ?, where the last digit can only be ?, notIf you useIt means that it will be triggered regardless of the day of the week, but this is not the case.
-: indicates a range. For example, if you use 5-20 in the Minutes field, it means that the trigger is triggered every minute from 5 minutes to 20 minutes.
/: Indicates that the trigger starts at the start time, and then triggers once at fixed intervals. For example, if 5/20 is used in the Minutes field, it means that the trigger is triggered once every 5 minutes, and once at 25, 45, etc.
,: indicates a list of enumeration values. For example, if 5,20 is used in the Minutes field, it means that the trigger is triggered once every minute at 5 and 20 minutes.
L: means the last, and can only appear in the DayofWeek and DayofMonth fields. If 5L is used in the DayofWeek field, it means that it will be triggered on the last Thursday.
W: indicates a valid working day (Monday to Friday), which can only appear in the DayofMonth field. The system will trigger the event on the valid working day closest to the specified date. For example, if 5W is used in DayofMonth, if the 5th is Saturday, the event will be triggered on the nearest working day: Friday, i.e. the 4th. If the 5th is Sunday, the event will be triggered on the 6th (Monday); if the 5th is a day between Monday and Friday, the event will be triggered on the 5th. Another point is that the nearest search of W will not cross the month.
LW: These two characters can be used together to indicate the last working day of a month, that is, the last Friday.
#: Used to determine the day of the week in each month. It can only appear in the DayofMonth field. For example, 4#2 means the second Wednesday of a month.
example:
0 0 2 1 * ? * means to schedule the task at 2:00 a.m. on the 1st of every month
0 15 10 ? * MON-FRI means the job is executed at 10:15 am every Monday to Friday
0 15 10 ? 6L 2002-2006 means that the action will be executed at 10:15 AM on the last Friday of every month from 2002 to 2006.
A cron expression has at least 6 (and possibly 7) time elements separated by spaces.
In order:
Seconds (0~59)
Minutes (0~59)
Hours (0~23)
Day (month) (0~31, but you need to consider the number of days in your month)
Month (0~11)
Day (week) (1~7 1=SUN or SUN, MON, TUE, WED, THU, FRI, SAT)
Year (1970-2099)
Each element can be a value (such as 6), a continuous interval (9-12), an interval (8-18/4) (/ means every 4 hours), a list (1,3,5), or a wildcard. Since the "day of the month" and "day of the week" elements are mutually exclusive, one of them must be set?
0 0 10,14,16 * * ? Every day at 10am, 2pm, 4pm
0 0/30 9-17 * * ? Every half hour during the 9-5 working hours
0 0 12 ? * WED means every Wednesday at 12 noon
"0 0 12 * * ?" Triggered at 12 noon every day
"0 15 10 ? * " Triggered every day at 10:15 am
"0 15 10 * * ?" Triggers at 10:15 AM every day
"0 15 10 * * ? *" triggers at 10:15 am every day
"0 15 10 * * ? 2005" triggers at 10:15 am every day in 2005
"0 * 14 * * ?" triggers every minute between 2:00 PM and 2:59 PM every day
"0 0/5 14 * * ?" triggers every 5 minutes between 2:00 PM and 2:55 PM every day
"0 0/5 14,18 * * ?" triggers every 5 minutes between 2pm and 2:55pm and between 6pm and 6:55pm every day
"0 0-5 14 * * ?" triggers every minute between 2pm and 2:05pm every day
"0 10,44 14 ? 3 WED" triggers at 2:10 pm and 2:44 pm on Wednesdays in March every year
"0 15 10 ? * MON-FRI" triggers at 10:15 am from Monday to Friday
"0 15 10 15 * ?" Triggered at 10:15 am on the 15th of every month
"0 15 10 L * ?" Triggers at 10:15 AM on the last day of every month
"0 15 10 ? * 6L" triggers at 10:15 am on the last Friday of every month
"0 15 10 ? * 6L 2002-2005" triggers at 10:15 AM on the last Friday of every month from 2002 to 2005
"0 15 10 ? * 6#3" triggers at 10:15 am on the third Friday of every month
Some subexpressions can contain ranges or lists
For example, the sub-expression (day (week)) can be "MON-FRI", "MON, WED, FRI", "MON-WED, SAT"
" character represents all possible values
therefore,""In the sub-expression (month) it means each month,""In the subexpression (day(week)) means every day of the week
The "/" character is used to specify the increment of the value
For example, "0/15" in the sub-expression (minutes) means starting from the 0th minute, every 15 minutes
"3/20" in the subexpression (minutes) means starting from the 3rd minute, every 20 minutes (it has the same meaning as "3, 23, 43")
The "?" character is only used in the day (month) and day (week) sub-expressions to indicate that no value is specified.
When one of the two sub-expressions is assigned a value, in order to avoid conflicts, the value of the other sub-expression needs to be set to "?"
The "L" character is used only in the day(month) and day(week) subexpressions and is an abbreviation for the word "last".
But its meaning is different in the two subexpressions.
In the day (month) sub-expression, "L" means the last day of the month
In the day (week) expression, "L" means the last day of the week, which is SAT.
If there is something specific before "L", it has other meanings.
For example: "6L" means the sixth last day of the month, and "FRIL" means the last Friday of the month.
Note: When using the "L" parameter, do not specify a list or range as this will cause problems
Special characters allowed in field values
Seconds 0-59, - * /
Points 0-59, - * /
Hours 0-23, - * /
Date 1-31 , - * ? / LWC
Month 1-12 or JAN-DEC, - * /
Monday-Saturday or Sunday-Saturday, - * ? / LC #
Year (optional) Leave blank, 1970-2099 , - * /
How to setup Cron Jobs in CentOS 8. If you have any questions on this, please comment below.

1/5 - (1 vote)

Leave a Reply

Your email address will not be published. Required fields are marked *