Cron Job
Knowledge

Cron Job

  • 2023-01-22 11:45:01

What is Cron Job?

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.

Advantages of using Cron Job

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.

Limitations when using Cron Job

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.

Cron Job Syntax

1 job will have 2 main components which are

  • Cron Expression: Indicates the job repetition cycle
  • Command: Command to execute

What is cron expression?

Cron Job 0

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:

  1. 1st character: Indicates execution at what minute?
  2. Second character: Indicates execution at what time?
  3. 3rd character: Indicates execution on which day of the month?
  4. 4th character: Indicates execution in which month of the year?
  5. 5th character: Indicates execution on which day of the week?

Above we already know that each position corresponds to 1 meaning, next we find out the ALLOWED VALUE RANGE at each position.

  1. 1st character: 0-59 (minutes)
  2. 2nd character: 0-23 (hour)
  3. 3rd character: 1-31 (date)
  4. 4th character: 1-12 (month)
  5. 5th character: 0-6 (Monday, 0 is Sunday)

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 * * 0every 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-5Monday 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 * *???

Author FlashPanel Admin
FlashPanel Admin
Joined in 3 years ago