FedoraのCrontabの動きを調べてみた

CentOSでは、/etc/crontabは、こうなっている。
時間が来たら、run-partsでディレクトリ内のファイルを実行する単純な仕組み。

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

 
でもFedoraの場合、このファイルが空っぽだった。
なのになぜかhourly,daily,weekly,monthlyが動いてる!
よく解らないので、調べてみた。
 

/etc/crontab

cronの設定ファイル。
Fedoraの場合、空っぽ。
 

ファイルの中身
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# 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
# |  |  |  |  |
# *  *  *  *  *  command to be executed

 

/etc/cron.d/0hourly

1時間置きにrun-partsで/etc/cron.hourly内のファイルが実行される。
 

ファイルの中身
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
01 * * * * root run-parts /etc/cron.hourly

 

/etc/cron.hourly/0anacron

前回の実行と日付が違う且つACアダプタが接続されている場合、anacronを実行。
 
/var/spool/anacron/cron.daily には前回実行された日付が入ってる。
/usr/bin/on_ac_power はACアダプタが接続されているかを返す。
 

ファイルの中身
#!/bin/bash
#in case file doesn't exist
if test -x /var/spool/anacron/cron.daily; then
    day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
    exit 0;
fi

# in case anacron is already running,
# there will be log (daemon won't be running twice).
if test -x /usr/bin/on_ac_power; then
    /usr/bin/on_ac_power &> /dev/null
    if test $? -eq 1; then
    exit 0
    fi
fi
/usr/sbin/anacron -s

 

/etc/anacrontab

anacronの設定ファイル。
時が来たらrun-partsでdaily,weekly,monthlyを実行。
 
多分、anacron自身が/var/spool/anacron/cron.dailyを更新する。
ファイル名は、job-identifierと一致する。
 

ファイルの中身
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

 

anacronのメリット

クライアントマシンのように、24時間稼働しっぱなしじゃないマシンでも、それっぽくdaily,weekly,monthlyを実行出来る。
(サーバ用途の場合、真っ昼間に実行されも困るし、きっちり時間を指定出来たほうがいいような…)
 

参考URL

ITmedia エンタープライズ : Linux Tips「anacronデーモンって何?」
http://www.itmedia.co.jp/help/tips/linux/l0560.html
 
anacron - RBB TODAY 百科事典
http://pedia.rbbtoday.com/svc_wiki/wikipedia/Anacron