[Linux] Points that got me hooked on CRON time format and execution interval
table of contents
Hello.
My name is Miyazaki from the System Solutions Department and I recently obtained AWS and GCP qualifications.
My LPIC, which was my goal until 202, is 000.
This time, I would like to write about the points I got into when setting CRON's time format and execution interval.
CRON time format
First of all, the times and dates that can be specified with CRON are as follows.
Reference: Man page of CRONTAB
field | Possible values |
minutes | 0-59 |
time | 0-23 |
day of the month | 1-31 |
month | 1-12 |
day of week | 0-7 |
A setting example is given below based on this.
# Run db_backup.sh at 04:00 0 4 * * * /home/centos/db_backup.sh
# rsync every 30 minutes */30 * * * * /home/centos/rsync.sh
# Count on the 15th of every month * * 15 * * /home/centos/count.sh
# Delete files every Monday * * * 1 * /home/centos/delete.sh
# Run every minute * * * * * /home/centos/1hunoki.sh
It's like this.
I'm sure there are many other things, but the one I got hooked on this time was when executing at 45 minute intervals.
When running every 45 minutes
Now, if I want to run a shell script every 45 minutes, how should I set it up?
At first, I tried setting this as I thought it would work.
#Run every 45 minutes */45 * * * * /home/centos/45hunoki.sh
Yes, this is incorrect.
Checking the cron log
[root@test-server ~]# cat /var/log/cron Sep 21 19:45:01 test-server CROND[3890]: (root) CMD (/home/centos/45hunoki.sh) Sep 21 20:00 :01 test-server CROND[3894]: (root) CMD (/home/centos/45hunoki.sh) Sep 21 20:45:01 test-server CROND[3899]: (root) CMD (/home/centos/45hunoki .sh)
It was running at 00 minutes and 45 minutes. why··
The reason for this is that the "*/" in "*/45" does not take into account all the "minutes" between 0 and 24 hours.
This way of saying it is difficult to understand.
In other words, you are not thinking 00:59,01:00,01:01.
If */45 is replaced, it becomes 00-59/45.
Therefore, it means specifying 00 and 45.
By the way, if you specify 01-12/3, you
are specifying 01,04,07,10.
*/9 means 0,9,18,27,...54,0.
Please note that the value returns to 0 after 54.
CRON's MAN has the following description.
You can also specify an interval value along with a range. Specifying ``/'' after a range will skip by the specified number within the range. For example, specifying ``0-23/2'' in the Hour field will cause the command to run every two hours.
Considering this, if you want to run at 45 minute intervals, the correct way to conclude is as follows.
*/45 */3 * * * $45hunoki.sh 30 1-22/3 * * * $45hunoki.sh 15 2-23/3 * * * $45hunoki.sh
If you do it every 45 minutes, you can create this kind of regularity.
・Executes every 3 hours from 0:00 to 00 minutes and 45 minutes
・Executes every 3 hours from 1:00 to 22:00 at 30 minutes
・Executes every 3 hours at 15 minutes from 2:00 to 23:00
minutes | 00 | 15 | 30 | 45 |
0 o'clock | 00:00 | 00:45 | ||
1 o'clock | 1:30 | |||
2 o'clock | 02:15 | |||
3 o'clock | 03:00 | 03:45 | ||
4 o'clock | 04:30 | |||
5 o'clock | 05:15 |
If you look at the table, you can see regularity whether it's every 40 minutes or every 50 minutes.
summary
*/45 I'm embarrassed that I thought it would be okay.
When I watched MAN again, I noticed a lot of things.
I recommend it because once you try writing it down on paper, you will notice the regularity easily.
That's it.