LoginSignup
6
5

More than 1 year has passed since last update.

logrotateが動かない (備忘録)

Last updated at Posted at 2021-09-08

logrotate とは

各種アプリケーションが吐き出すlogファイルを圧縮したりローテーションしたり古いものを削除したりするアプリケーション。

環境

  • Fedora Server 31 (5.8.18-100.fc31.x86_64)

何が起こったのか

ログファイルのローテーションが行われない。

logrotateの設定ファイル(/etc/logrotate.conf,/etc/logrotate.d/*.conf)の記述内容は正しく、
sudo logrotate -d /etc/logrotate.confでデバッグしても問題ない。
また、sudo logrotate -f /etc/logrotate.conf で手動で強制的に実行すると正常にローテーションされるにもかかわらず、自動でローテーションされない。

解決方法

logrotate.timer サービスが動作していなければ動かす。

$ sudo systemctl status logrotate.timer 
● logrotate.timer - Daily rotation of log files
   Loaded: loaded (/usr/lib/systemd/system/logrotate.timer; disabled; vendor preset: disabled)
   Active: inactive (dead)
  Trigger: n/a
     Docs: man:logrotate(8)
           man:logrotate.conf(5)

$ sudo systemctl start logrotate.timer 

$ sudo systemctl status logrotate.timer 
● logrotate.timer - Daily rotation of log files
   Loaded: loaded (/usr/lib/systemd/system/logrotate.timer; disabled; vendor preset: disabled)
   Active: active (waiting) since Wed 2021-09-08 09:18:15 JST; 3s ago
  Trigger: Thu 2021-09-09 00:00:00 JST; 14h left
     Docs: man:logrotate(8)
           man:logrotate.conf(5)

$ sudo systemctl enable logrotate.timer 

次はいつ動くか知りたい

$ sudo systemctl list-timers
NEXT                         LEFT          LAST                         PASSED    UNIT                         ACTIVATES
Wed 2021-09-08 09:47:13 JST  14min left    Wed 2021-09-08 08:47:13 JST  46min ago dnf-makecache.timer          dnf-makecache.service
Wed 2021-09-08 11:25:13 JST  1h 51min left Tue 2021-09-07 11:25:13 JST  22h ago   systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.s>
Thu 2021-09-09 00:00:00 JST  14h left      n/a                          n/a       logrotate.timer              logrotate.service
Thu 2021-09-09 00:00:00 JST  14h left      Wed 2021-09-08 00:00:13 JST  9h ago    mlocate-updatedb.timer       mlocate-updatedb.service
Thu 2021-09-09 00:00:00 JST  14h left      Wed 2021-09-08 00:00:13 JST  9h ago    unbound-anchor.timer         unbound-anchor.service

5 timers listed.
Pass --all to see loaded but inactive timers, too.

上記例ではThu 2021-09-09 00:00:00 JSTにキックされる

タイマーの設定はどこ?

/etc/systemd/system/timers.target.wants/logrotate.timerで設定されています

logrotate.timer
[Unit]
Description=Daily rotation of log files
Documentation=man:logrotate(8) man:logrotate.conf(5)

[Timer]
OnCalendar=daily
AccuracySec=1h
Persistent=true

[Install]
WantedBy=timers.target

なぜ起きたのか

  • logrotateはcron.dailyで動くものだと思い込んでいた。(実際、ググると「cronで動く」と記載のあるサイトが多数ヒットする)
  • そして、設定に問題があるものと思い設定ファイルをこねくり回していた。

参考資料

ArchLinux Wiki - logrotate

6
5
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
6
5