[Linux] Problems with 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 certifications.
My LPIC goal was 202, but now I'm 000.
This time I would like to write about the points I encountered when setting the CRON time format and execution interval
CRON time format
First of all, the time and date 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 |
Based on this, a configuration example is provided below
# Execute 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 1 minute * * * * * /home/centos/1hunoki.sh
It looks like this.
There are probably many other issues, but the problem I ran into this time was running it every 45 minutes.
When executed every 45 minutes
So, how should you set up a shell script to run every 45 minutes?
First, I set it up like this, which I thought 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 executed at 00 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, it does not consider 00:59, 01:00, 01:01.
*/45 becomes 00-59/45 when replaced.
Therefore, it means that 00 and 45 are specified.
By the way, if you specify 01-12/3,
you are specifying 01, 04, 07, and 10.
*/9 gives 0, 9, 18, 27, ... 54, 0.
Note that after 54 it returns to 0.
The CRON MAN page states the following:
Interval values may also be specified with ranges. A ``/'' after a range will skip the specified number of times in the range. For example, specifying ``0-23/2'' in the hour field will cause the command to be run every two hours
Taking this into consideration, if you want to run it every 45 minutes, the correct conclusion is to do the following:
*/45 */3 * * * $45hunoki.sh 30 1-22/3 * * * $45hunoki.sh 15 2-23/3 * * * $45hunoki.sh
If you set it to every 45 minutes, you can create the following patterns:
・Execute at 00 and 45 minutes every 3 hours from midnight
・Execute at 30 minutes every 3 hours from 1:00 to 22:00
・Execute at 15 minutes every 3 hours 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 |
When you make a table, you can see the pattern whether it's every 40 minutes or every 50 minutes
summary
*/45 I'm embarrassed that I thought it would be okay.
Looking at the MAN again, I noticed a lot of things.
I recommend writing it down on paper once, as it's surprisingly easy to notice the patterns.
That's all
4