Just now, I had a task to do a scheduler to clear junk data, run once a month. Before I run cronjob with the server’s support tool, there is UI so I don’t care what mechanism it configures, just bury right-day-month-year-hour-minute-second in the right box, click OK and it is automatically generate configuration.
However this time I have to touch the configuration for cronjob. Convenient to learn it should note a post
Cronjob configuration structure
Cronjob usually takes the form
1 2 | cron = "X X X X X" (5 kí tự) |
good
1 2 | cron = "X X X X X X" (6 kí tự) |
For 5 characters, it is:
1 2 | <phút> <giờ> <ngày trong tháng> <tháng> <ngày trong tuần> |
For 6 characters, add a second:
1 2 | <giây> <phút> <giờ> <ngày> <tháng> <ngày trong tuần> |
The X characters are replaced by one of the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 | * : bất kì giá trị nào , : danh sách các giá trị - : khoảng giá trị / : theo bước nhảy 0-59: đối với các giá trị giây, phút 0-23: đối với giá trị giờ 1-31: đối với giá trị ngày 1-12: đối với giá trị tháng JAN-DEC: đối với giá trị tháng 0-6: đối với ngày trong tuần SUN-SAT: đối với ngày trong tuần |
First glance at this pile, I suddenly thought: easy vkl. Now want to test for it to run every 2 seconds to test the scheduler then buried config
1 2 | cron = "2 * * * * *" |
There is no difficult mother: v. However, when running the test, it was wrong. It runs 1 minute 1 time. That’s the second second of every minute , not every 2 seconds. Thus, the configuration of cron must be exactly:
1 2 | <giây trong phút> <phút trong giờ> <giờ trong ngày> <ngày trong tháng> <tháng trong năm> <ngày trong tuần> |
Config 2s 1 times what type?
Let’s take a look at the alternate characters of X, we can configure to run every 2 seconds using the following methods:
Use “,”
The “,” symbol represents the list of values. This means that if you want to run every 2 seconds, you list the second values that appear every second.
1 2 | cron= "0,2,4,6,8,10,12,14,16,18,20....58 * * * * *" |
This method is quite manual, should only be used when you use to configure a few special values, there are no rules.
Use “-“
The “-” sign represents a value range. For example, if you want to run at seconds 1,2,3,4,5,6, you can use “-” for brevity, instead of “,”. That is:
1 2 | cron="1-6 * * * * *" |
Of course, this config cannot be used for 2s at a time. By the way, I recommend it
Use “/”
The “/” symbol represents the jump of the value, calculated from 0. Assuming the character at the position of seconds, I configured:
1 2 3 4 5 6 | */1 => bước nhảy 1, tính từ 0: 0,1,2,3,4,...,59 */2 => bước nhảy 2, tính từ 0: 0,2,4,6,8,...,58 */5 => bước nhảy 5, tính từ 0: 0,5,10,15,...,55 |
You can also change the *
symbol as the starting position of the jump. For example
1 2 3 4 5 6 7 8 | 5/1 => bước nhảy 1, tính từ 5: 5,6,7,8,...59 5/8 => bước nhảy 8, tính từ 5: 5,13,21,29 13/4 => bước nhảy 4, tính từ giây thứ 13 blah,blah.... |
However, this is a “non-standard config”, which is not a standard config so it may not work on some cronjob systems.
Thus, to config 2s / times, the “redundant” config:
1 2 | cron="*/2 * * * * *" |
Create cronjob online
There are several websites that allow you to create cronjob online, including explanations. It is easy to understand. I use the following two pages:
If there is anything wrong or not optimal in the post, please comment let me know ^^ Thanks for reading ^^
Source of articles from my blog: https://minhphong306.wordpress.com/2018/09/03/tutorial-tim-hieu-cac-thong-so-cau-hinh-cronjob/