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

[Osaka/Yokohama] 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】CRONの時刻書式と実行間隔でハマったポイント

こんにちは。

最近AWSとGCPの資格を取りました、システムソリューション部の宮崎です。
202まで目標だったLPICは000です。

今回はCRONの時刻書式と実行間隔を設定する時にハマったポイントについて書いていきたいと思います。

CRONの時刻書式

まず最初に、CRONで指定できる時刻や日付は以下の通りです。
参考 : Man page of CRONTAB

フィールド 指定可能な値
0-59
0-23
月内日 1-31
1-12
曜日 0-7

これを元に以下に設定例を記載します。

# db_backup.shを04:00に実行する
0 4 * * *       /home/centos/db_backup.sh
# 30分毎にrsyncする
*/30 * * * *      /home/centos/rsync.sh
# 毎月15日に集計する
* * 15 * *      /home/centos/count.sh
# 毎週月曜日にファイルを削除する
* * * 1 *       /home/centos/delete.sh
# 1分毎に実行する
* * * * *     /home/centos/1hunoki.sh

こんな感じですね。
他にもいろいろあるかと思いますが、今回ハマった点というのが45分間隔で実行する場合でした。

45分間隔で実行する場合

さて、45分間隔でシェルスクリプトを実行したい場合どのように設定すればよいでしょうか。
最初はこれでいけそうだなと設定してみました。

#45分間隔で実行する
*/45 * * * *     /home/centos/45hunoki.sh

はい、これ不正解です。

cronのログを確認すると

[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)

00分と45分に実行されていました。なんで・・

こうなる理由ですが、「*/45」の「*/」は決して0-24時の間のすべての「分」を考慮しているわけではないからです。
この言い方わかりにくいですね。
つまり、00:59,01:00,01:01と考えているわけでないということです。

*/45は置き換えると、00-59/45となります。
よって00と45を指定している意味になります。

ちなみに、01-12/3とした場合
01,04,07,10を指定していることになります。

*/9 であれば、0,9,18,27,・・・54,0です。
注意点ですが54の次は0に戻ります。

CRONのMANには以下のような記載があります。

間隔値を範囲とともに指定することもできる。 範囲の後に ``/'' と指定すると、 範囲内で指定数値ずつ飛ばすことになる。 例えば「時」フィールドに ``0-23/2'' と指定すると、 コマンドは 2 時間おきに実行される

Man page of CRONTAB

これを考慮した上で45分間隔で実行したい場合、結論から言えば以下のようにするのが正しいです。

*/45 */3 * * *        $45hunoki.sh
30 1-22/3 * * *       $45hunoki.sh
15 2-23/3 * * *       $45hunoki.sh

45分おきにすると、このような規則性ができるんですね。
・0時から3時間おきに00分と45分に実行
・1時から22時まで3時間おきに30分に実行
・2時から23時まで3時間おきに15分に実行

00 15 30 45
0時台 00:00 00:45
1時台 1:30
2時台 02:15
3時台 03:00 03:45
4時台 04:30
5時台 05:15

表にしてみると、40分毎でも50分毎でも規則性がみえてきます。

まとめ

*/45大丈夫だろうと思っていた自分が恥ずかしいです。
改めてMANを見ると気づくことがたくさんありました。
一度紙に書いてみたりすると、案外簡単に規則性に気付くのでオススメします。

以上です。

この記事がお役に立てば【 いいね 】のご協力をお願いいたします!
4
読み込み中...
4 票, 平均: 1.00 / 14
51,449
X facebook はてなブックマーク pocket
[2024.6.30 CentOS support ended] CentOS server migration solution

[2024.6.30 CentOS support ended] CentOS server migration solution

[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

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

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