[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

[Linux] Points that got me hooked on CRON time format and execution interval

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.

Man page of CRONTAB

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.

If you found this article helpful , please give it a like!
4
Loading...
4 votes, average: 1.00 / 14
54,008
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

Kenta Miyazaki

I joined Beyond in 2017 as a new graduate.

We provide 24-hour, 365-day operation, maintenance, and monitoring services for servers and clouds used by companies that primarily provide web-based services.
I belong to the System Solutions Department, and my job is to improve Beyond's operations so that our customers can focus on their business.

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