What is a Cron Job?

A scheduled task is a task that executes a planned work at an agreed time. This is superficial. In Linux, we often use cron servers to do this. The cron server can perform specific tasks based on the time agreed in the configuration file.

cron

A scheduled task is a task that executes a planned work at an agreed time. This is superficial. In Linux, we often use cron servers to do this. The cron server can perform specific tasks based on the time agreed in the configuration file.
Chinese name
Scheduled Tasks
Foreign name
cron
Pronunciation
'krn
Features
Perform specific tasks based on agreed time
Let s talk about cron first: Pronunciation ['kr: n] / [' krn]
In Linux systems, cron tasks are generally undertaken by cron. We can set cron to start automatically when it is turned on. After cron is started, it will read all its configuration files (global configuration file / etc / crontab, and each user's scheduled task configuration file), and then cron will call the task in time according to the command and execution time.
cron is a scheduled execution tool under Linux that can run jobs without human intervention. Cron is a built-in service of Linux, but it does not start automatically. You can use the following methods to start and stop this service:
/ sbin / service crond start // Start the service
/ sbin / service crond stop // Close the service
A 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: four characters ",-* /" can appear, valid range is 0-59
Minutes: ",-* /" four characters, valid range is an integer from 0-59
Hours: ",-* /" four characters can appear, the valid range is an integer from 0-23
DayofMonth: eight characters, ",-* /? LW C" can appear, the valid range is an integer from 0-31
Month: Four characters ",-* /" can appear, valid integer is 1-12 or JAN-DEc
DayofWeek: can appear ",-* /? LC #" eight characters, the valid range is an integer of 1-7 or two ranges of SUN-SAT. 1 for Sunday, 2 for Monday, and so on
Year: Four characters ",-* /" can appear, valid range is 1970-2099
Each field uses numbers, but the following special characters can also appear, their meanings are:
(1) *: Matches any value in this field. If * is used in the Minutes field, it means that the event will be triggered every minute.
(2) ?: Can only be used in the two domains of DayofMonth and DayofWeek. It also matches any value of the domain, but it doesn't. Because DayofMonth and DayofWeek affect each other. For example, if you want to trigger the scheduling on the 20th of each month, no matter what day of the week the 20th is, you can only use the following expression: 13 13 15 20 *?, Where the last one can only be used? , And you cannot use *, if you use * to indicate that it will be triggered regardless of the day of the week, it is actually not the case.
(3)-: indicates the range. For example, 5-20 is used in the Minutes domain, which means that it is triggered every minute from 5 minutes to 20 minutes.
(4) /: indicates that the trigger starts at the start time, and then triggers at a fixed time. For example, using 5/20 in the Minutes domain means triggering once every 5 minutes, and 25, 45, etc. once.
(5) ,: Lists enumerated values. For example: using 5,20 in the Minutes domain means triggering once in the 5th and 20th minutes.
(6) L: It means that it can only appear in the DayofWeek and DayofMonth domains at the end. If you use 5L in the DayofWeek domain, it means that it will be triggered on the last Thursday.
(7) W: indicates a valid working day (Monday to Friday), which can only appear in the DayofMonth field. The system will trigger an event on the nearest valid working day from the specified date. For example: 5W is used in DayofMonth. If the 5th is Saturday, it will be triggered on the nearest working day: Friday, that is, the 4th. If the 5th is Sunday, it will be triggered on the 6th (Monday); if the 5th is one of Monday to Friday, it will be triggered on the 5th. In addition, W's recent search will not cross the month
(8) LW: These two characters can be used together to indicate the last working day of a month, that is, the last non-Saturday weekend date.
(9) #: It is used to determine the day of the week of each month and can only appear in the DayofWeek domain. For example, 4 # 2 means the second Wednesday of a month.
A few examples:
0 0 2 1 *? * Means to schedule the task at 2 am on the 1st of every month
0 15 10? * MON-FRI means that the job will be executed at 10:15 am every day from Monday to Friday
0 15 10? * 6L 2002-2006 represents the last Friday of each month 2002-2006 at 10:15 am
0 0 10,14,16 * *? Every day at 10 am, 2 pm, and 4 pm
0 0/30 9-17 * *? Every half an hour during 9 to 5 working hours
0 0 12? * WED means every Wednesday at 12 noon
"0 0 12 * *?" Triggered every day at 12 noon
"0 15 10? * *" Triggered every day at 10:15 AM
"0 15 10 * *?" Triggered every day at 10:15 AM
"0 15 10 * *? *" Triggered every day at 10:15 AM
"0 15 10 * *? 2005" Triggered at 10:15 AM every day in 2005
"0 * 14 * *?" Triggers every 1 minute from 2pm to 2:59 pm
"0 0/5 14 * *?" Triggers every 5 minutes every day from 2pm to 2:55 pm
"0 0/5 14,18 * *?" Triggered every 5 minutes from 2pm to 2:55 pm and from 6pm to 6:55 pm daily
"0 0-5 14 * *?" Triggers every 1 minute from 2pm to 2:05 pm
"0 10,44 14? 3 WED" Triggered at 2:10 and 2:44 pm on Wednesdays in March each year
"0 15 10? * MON-FRI" Triggered 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 *?" Triggered at 10:15 AM on the last day of each month
"0 15 10? * 6L" Triggered at 10:15 AM on the last Friday of the month
"0 15 10? * 6L 2002-2005" Triggered at 10:15 AM on the last Friday of each month from 2002 to 2005
"0 15 10? * 6 # 3" Triggered on the third Friday of every month at 10:15 AM
Kron, a unit of time, one kron is one million years

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?