LoginSignup
0
0

More than 1 year has passed since last update.

VirtualBox CentOS7 cronについてさらに調べてみた

Last updated at Posted at 2022-04-29

はじめに

cronは設定ファイル変更後、サービス再起動は不要です。
理由はcronデーモンが設定ファイル変更を自動で検出します。
※有識者にご教示いただきました。

前回、以下の記事でcrontabについて検証しました。
https://qiita.com/gama1234/items/2f52ac806512a28868d5

crontabと/etc/cron.x配下のcron設定は別物です。
どちらかにcronの設定をしてください

今回は/etc/cron.hourlyと/etc/cron.dについて調査しました。

動作環境

VirtualBox
Windows端末
CentOS7使用
※ダウンロードに時間がかかるため、最低限の機能が入っている
CentOSのminimalISOをダウンロードしました。

検証 /etc/cron.hourly配下に格納したスクリプトが動くか検証した

事前準備

vi /etc/crontabを開くと、# run-partsの処理の/etc/cron.hourly~/etc/cron.monthly
の設定が入っていなかったため追加した。

vi /etc/crontab
[root@localhost cron.hourly]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

※以下の設定が入ってなかったため追加
# run-parts
※毎時15分に/etc/cron.hourlyフォルダ配下にあるスクリプトを実行する。
15 * * * * root run-parts /etc/cron.hourly  
02 9 * * * root run-parts /etc/cron.daily
22 9 * * 0 root run-parts /etc/cron.weekly
42 9 30 * * root run-parts /etc/cron.monthly

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

/etc/cron.hourlyに検証用の簡単なスクリプトを配置した

スクリプトは実行権限ない場合、cronで設定しても動作しないため注意

[root@localhost cron.hourly]# pwd
/etc/cron.hourly
[root@localhost cron.hourly]# ls -tlr
合計 8
-rwxr-xr-x. 1 root root 392  1月 14 01:52 0anacron
-rw-r--r--. 1 root root  51  4月 29 09:02 test
[root@localhost cron.hourly]#
※日付をファイルに出力するスクリプト
[root@localhost cron.hourly]# cat test
#!/bin/sh
echo `date` > /tmp/date_cron.d_hours.log

[root@localhost cron.hourly]# chmod +x test #実行権限を付ける。
[root@localhost cron.hourly]# ls -tlr
合計 8
-rwxr-xr-x. 1 root root 392  1月 14 01:52 0anacron
-rwxr-xr-x. 1 root root  51  4月 29 09:02 test
[root@localhost cron.hourly]#

ログから実行を確認した

# less /var/log/cron
Apr 29 09:15:01 localhost CROND[2552]: (root) CMD (run-parts /etc/cron.hourly)
Apr 29 09:15:01 localhost CROND[2553]: (root) CMD (echo `date` >> /tmp/date_root.log)
Apr 29 09:15:01 localhost run-parts(/etc/cron.hourly)[2552]: starting 0anacron
Apr 29 09:15:01 localhost run-parts(/etc/cron.hourly)[2570]: finished 0anacron
Apr 29 09:15:01 localhost run-parts(/etc/cron.hourly)[2552]: starting test ※testスクリプトが動作していることを確認
Apr 29 09:15:01 localhost run-parts(/etc/cron.hourly)[2589]: finished test

スクリプトのファイルの出力を確認した

[root@localhost tmp]# cat /tmp/date_cron.d_hours.log
2022年 4月 29日 金曜日 09:15:01 JST
[root@localhost tmp]#

最後に/etc/cron.dも動作を確認してみた

/etc/cron.d配下にデフォルトで存在する0hourlyと同じ設定で、testファイルを作成した。

[root@localhost cron.d]# pwd
/etc/cron.d
[root@localhost cron.d]# ls -ltr
合計 8
-rw-r--r--. 1 root root 128  1月 14 01:52 0hourly
-rw-r--r--. 1 root root 142  4月 29 10:26 test  ※このファイルはスクリプトではないため、実行権限がなくても動作する。
[root@localhost cron.d]#
[root@localhost cron.d]# cat test
※SHELL~MAILTOは、0hourlyからコピーした。
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
※1分ごとに日時を出力するコマンドを設定
*/1 * * * * root echo `date` >> /tmp/date_etc_cron.d.log

/etc/cron.d/testの出力ファイル名で検索し、ログの量が多いため、先頭の3行のみ表示した。

[root@localhost cron.d]# cat /var/log/cron | grep "/tmp/date_etc_cron.d.log" | head -n3
Apr 29 10:27:01 localhost CROND[4367]: (root) CMD (echo `date` >> /tmp/date_etc_cron.d.log)
Apr 29 10:28:01 localhost CROND[4395]: (root) CMD (echo `date` >> /tmp/date_etc_cron.d.log)
Apr 29 10:29:01 localhost CROND[4425]: (root) CMD (echo `date` >> /tmp/date_etc_cron.d.log)

/etc/cron.d/testの出力ファイルの中身を確認した。

[root@localhost tmp]# cat /tmp/date_etc_cron.d.log | head -n3
2022年 4月 29日 金曜日 10:27:01 JST
2022年 4月 29日 金曜日 10:28:01 JST
2022年 4月 29日 金曜日 10:29:01 JST
[root@localhost tmp]#

分かったこと

CentOSのminimalISOからCentOS7をダウンロードしたのが原因の可能性はありますが、
デフォルトでは、/etc/crontabに/etc/cron.hourlyのcron設定が入っていないことを確認した。
上記の設定せずに、/etc/cron.hourly配下にスクリプトを配置しても動作しないことが分かりました。
また、/etc/cron.hourlyにスクリプトを配置する場合、スクリプトに実行権限を付け忘れないようにする必要があります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0