[Linux] Problems with CRON time format and execution interval

Hello

I'm Miyazaki from the System Solutions Department, and I recently obtained AWS and GCP certifications.
My LPIC score was 000, which was my goal of 202.

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, 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

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

That's basically it.
I'm sure there are other issues, but the problem I encountered this time was when running it at 45-minute intervals.

When executed every 45 minutes

So, how do you configure it to run a shell script every 45 minutes? I
initially tried setting it up this way, thinking it might 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 this happens is that the "*/" in "*/45" does not take into account all the "minutes" between 0:00 and 24:00.
This explanation is a bit confusing.
In other words, it does not take into account 00:59, 01:00, 01:01, etc.

*/45, when replaced, becomes 00-59/45.
Therefore, it means specifying 00 and 45.

Incidentally, if you specify 01-12/3,
you are specifying 01, 04, 07, and 10.

If you use */9, the values ​​will be 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

Man page of CRONTAB

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 run it every 45 minutes, you get this kind of pattern:
- Runs at 00 and 45 minutes past the hour, every 3 hours from 0:00
- Runs at 30 minutes past the hour, every 3 hours from 1:00 to 22:00
- Runs at 15 minutes past the hour, 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

I'm ashamed of myself for thinking that */45 would be okay.
Looking at MAN again, I realized there were many things I hadn't noticed.
I recommend writing it down on paper once; you'll find that the patterns are surprisingly easy to spot.

That's all

If you found this article helpful,please give it a "Like"!
4
Loading...
4 votes, average: 1.00 / 14
57,999
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Kenta Miyazaki

I joined Beyond as a new graduate in 2017

We provide 24/7/365 operation, maintenance, and monitoring services for servers/clouds primarily used by companies that develop web-based services. I
belong to the System Solutions Department, and my work is driven by the desire to improve Beyond's operations so that our customers can focus on their own businesses.

Certifications: AWS Certified Solutions Architect, GCP Professional Cloud Architect, Linuc1