システムのcron設定
ユーザーのcronに対し、システム用のcrontabも用意されています。/etc/crontabファイルはというファイルに対し、そこから/etc/cron.*ディレクトリ配下に置かれたファイルを呼び出します。
システムのcrontabの書式について
ユーザーのcrontabについてはユーザー名が明らかでしたが、システムのcrontabにはユーザー名が明記されています。
書式
分 時 日 月 曜日 ユーザー名 コマンド
crontab関連のファイル、ディレクトリ について
ファイル/ディレクトリ | 説明 |
/etc/crontab | システムのcrontabファル。 |
/etc/cron.hourly/ | 1時間に1度実行したいcronジョブを記載したファイルを格納するディレクトリ。 |
/etc/cron.daily/ | 1日に1度実行したいcronジョブを記載したファイルを格納するディレクトリ。 |
/etc/cron.weely/ | 1週間に1度実行したいcronジョブを記載したファイルを格納するディレクトリ。 |
/etc/cron.monthly/ | 1ヶ月に1度実行したいcronジョブを記載したファイルを格納するディレクトリ。 |
/etc/cron.d/ | cronジョブを記載したファイルを格納するディレクトリ。 |
検証
※以下の検証はCentOS8で検証しています。
検証するのに1日とか1週間はかけられないので、1時間あたりで実行する/etc/cron.houlyの検証をします。
まずは、/etc/cron.hourlyから。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[root@localhost cron.hourly]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # 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 ###毎時47分に/cron/hourly配下にあるスクリプトを実行。 47 * * * * root run-parts /etc/cron.hourly |
run-partsについて
run-partsは、指定したディレクトリ配下にあるファイルを全て実行してくれるコマンドです。
run-partsの実行例
1 2 3 4 5 |
[root@localhost localhost]# ls -l /home/localhost/ -rwxr-xr-x. 1 root root 31 9月 7 13:18 EchoFile01.sh -rwxr-xr-x. 1 root root 31 9月 7 13:18 EchoFile02.sh -rwxr-xr-x. 1 root root 31 9月 7 13:19 EchoFile03.sh |
実行するファイルの中身を見ます。
1 2 3 4 5 6 7 |
[root@localhost localhost]# cat /home/localhost/EchoFile01.sh echo "This file is EchoTest01" [root@localhost localhost]# cat /home/localhost/EchoFile02.sh echo "This file is EchoTest02" [root@localhost localhost]# cat /home/localhost/EchoFile03.sh echo "This file is EchoTest03" |
実行されると、ファイル名を表示するスクリプトです。では、run-partsで実行します。
1 2 3 4 5 6 7 8 |
[root@localhost localhost]# run-parts /home/localhost/ /home/localhost/EchoFile01.sh: This file is EchoTest01 /home/localhost/EchoFile02.sh: This file is EchoTest02 /home/localhost/EchoFile03.sh: < |