The cron command line utility, also known as cron job is a job scheduler on Unix-like operating systems. The user sets up and maintains a software environment that uses cron to schedule jobs to run periodically at fixed times, dates, or intervals.
One of the woes of sysadmins must be the task of working day and night. Some tasks need to be done overnight, or must be done on weekends. So we have to spend free time every night to run commands and scripts overtime? Or do you have to stay up all night running backups or updates?
Thanks to Cron Job , you will save a lot of time when scheduling repetitive tasks, no need to try to memorize and create cron jobs again and again.
Cron Jobs can only execute statements with a cycle of 1 minute or more, in case you want to perform repetitive jobs with a cycle less than 1 minute, it will not be possible.
1 job will have 2 main components which are
As you can see in the picture above, * * * * *
it is the Cron expression
Cron expression is 5 characters separated by 1 space, each character represents a different meaning, let's find out:
Above we already know that each position corresponds to 1 meaning, next we find out the ALLOWED VALUE RANGE at each position.
After understanding the range of values, we continue to learn about SPECIAL CHARACTERS
" * ": All values within the allowed range. For example, if you use character 1, it means every minute, character 2 means every hour, ... every day, every month.
" , ": Used to list values. For example, if you want to execute at 7am and 7pm, at the 2nd character you will enter 7.19
" - ": Indicates the range of values. For example, if you want to execute between 7am and 7pm, the 2nd character you will enter is 7-19
" / " : Indicates the value jump. For example, if you want to execute every 5 minutes, at the 1st character you will enter 0/5 (start at 0 minutes, then every 5 minutes)
So after combining the above 3 factors, you can build a cron expression as you like:
Eg:
* * * * *
every minute, every hour, every day, every month, every day of the week. In short, every minute until the server crashes
0 * * * *
every hour
0 0 * * *
every 12 o'clock at night
0 0 * * 0
every Sunday, at 12 o'clock at night
0 0 1 * *
on the 1st of every month, at 12 o'clock at night
0 22 * * 1-5
Monday to Friday, at 10pm.
23 0-20/2 * * *
from 0 to 20 hours (at 23 minutes), 2 hours apart.
Do you want to try?
0 0,12 1 */2 *
???
0 4 8-14 * *
???
0 0 1,15 * 3
???
5 0 * 8 *
???
15 14 1 * *
???