LoginSignup
5
4

More than 3 years have passed since last update.

anacronによるlogrotate等を1日の最後(23:59)に最小限の設定変更で実行させる

Posted at

anacronによるlogrotate等を1日の最後(23:59)に最小限の設定変更で実行させる

概要

anacronは一般的には下記のような使われ方をする。

  • ○時台のどこかで実行
  • 実行できなかった場合は○時台まで1時間に一度リトライ

しかしlogrotateなどをanacron実行する場合、決まった時間かつ、もっと言えば一日の最後に実行してくれるのが望ましい。
一例として毎日23:59に実行する設定方法を紹介。

/etc/anacrontab
# /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
RANDOM_DELAY=0
# the jobs will be started during the following hours only
#START_HOURS_RANGE=3-22
START_HOURS_RANGE=23-24

#period in days   delay in minutes   job-identifier   command
#1      5       cron.daily              nice run-parts /etc/cron.daily
1       58      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
/etc/cron.d/0hourly
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly

設定値の解説

RANDOM_DELAY

最大で設定値の分数が実行をランダムで遅延させるが、今回は同じ時間に実行させたいため、0を設定。

START_HOURS_RANGE

23時に実行する。(24を設定する理由は後述。)

delay in minutes

※/etc/cron.d/0hourlyにて、毎時のcron起動がx時1分に設定されていると仮定する。
RANDOM_DELAYに加え、delay in minutesで設定した分数が、実行を固定で遅延させる。
上述のとおり、x時1分にanacronのjobが起動する設定となっているため、今回は+58して59分の実行としたい。
したがって、「period in days」が1に設定された日時実行設定のdelay in minutesを58に設定する。

補足

START_HOURS_RANGEは単一値の指定ができず、かつ日付を跨ぐ場合でも必ずstart < endの関係である必要がある。
以下の値指定では実行がされないので注意

START_HOURS_RANGE=23-0
START_HOURS_RANGE=23-23
START_HOURS_RANGE=23
5
4
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
5
4