Amazon Linux 2023でlogrotateが動作しない場合の原因と対処

普段Windowsしか知らない人間がLinuxを使ってみて躓いたので、その原因と対処を書いておきます。
ログローテーションの対象ログが、複数の設定ファイルで同じものが重複していると動作しません。

Linuxでは、ログファイルを毎日・毎週などで新しいファイルにして、ログ調査などを行いやすくすることが一般的です。そのやり方として、logrotateというデーモンで、指定したタイミングでログファイルを新しくしたり圧縮したりできます。

昔のLinuxでlogrotateは、cronで実行するものでしたが、最近はsystemdのタイマーで実行するようです。今回ローテーションさせるため、/etc/logrotate.d/rsyslogにローテーションさせたいファイルを書いて、1日放置してみたところ動作せずログがそのままでした。

タイマーが正しく動いていないのかと下記コマンドを実行。

$ sudo systemctl enable logrotate.timer

そのあとに有効なタイマーを確認するために下記を実行したところ、logrotatedがありませんでした。

$ sudo systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
Mon 2025-02-17 10:06:28 JST 24s left Mon 2025-02-17 10:05:26 JST 38s ago refresh-policy-routes@enX0.timer refresh-policy-routes@enX0.service
Mon 2025-02-17 13:47:26 JST 3h 41min left Sun 2025-02-16 13:47:26 JST 20h ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2025-02-17 20:10:44 JST 10h left Mon 2025-02-17 00:31:26 JST 9h ago update-motd.timer update-motd.service
Mon 2025-02-24 00:47:48 JST 6 days left Mon 2025-02-17 00:43:26 JST 9h ago fstrim.timer fstrim.service

4 timers listed.
Pass –all to see loaded but inactive timers, too.

ステータスを確認しても、Activeとなっていませんでした。

有効にならない理由が分からないため、logrotateの設定が正しいか検証コマンドを実行してみることに。

$ sudo logrotate -dv /etc/logrotate.conf
WARNING: logrotate in debug mode does nothing except printing debug messages! Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.conf
including /etc/logrotate.d
reading config file btmp
reading config file chrony
reading config file dnf
reading config file rsyslog
reading config file squid
error: squid:1 duplicate log entry for /var/log/squid/access.log
error: found error in file squid, skipping
reading config file wtmp
reading config file zabbix-agent
Reading state from file: /var/lib/logrotate/logrotate.status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state

Handling 7 logs
(以下略)

そうすると、エラーとして「error: squid:1 duplicate log entry for /var/log/squid/access.log」が出ており、squidのログファイルを複数書いてしまっているのが原因でした。なんという凡ミス。

logrotateの設定ファイルを編集して、logrotateのstartとstatusをもう一度確認します。「Active: active」と表示されて正常に動作しました。

$ 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; enabled; preset: enabled)
Active: active (waiting) since Mon 2025-02-17 11:12:17 JST; 2s ago
Trigger: Tue 2025-02-18 00:00:00 JST; 12h left
Triggers: ● logrotate.service
Docs: man:logrotate(8)
man:logrotate.conf(5)

Feb 17 11:12:17 al2023sv systemd[1]: Started logrotate.timer – Daily rotation of log files.

タイマーが開始されたので、ログローテーションされたかをlsで確認します。

$ sudo ls -al /var/log
total 4716
drwxr-xr-x 8 root root 4096 Feb 17 10:19 .
drwxr-xr-x. 19 root root 269 Feb 13 10:49 ..
lrwxrwxrwx. 1 root root 39 Dec 19 16:08 README -> ../../usr/share/doc/systemd/README.logs
drwx——. 9 root root 23 Feb 13 10:21 audit
-rw-rw—-. 1 root utmp 384 Feb 14 13:14 btmp
drwxr-x—. 9 chrony chrony 108 Feb 17 10:19 chrony
-rw-r—–. 1 root adm 11403 Feb 14 13:45 cloud-init-output.log
-rw-r—–. 1 root adm 467265 Feb 14 13:45 cloud-init.log
-rw-r–r–. 1 root root 598495 Feb 17 00:45 dnf.librepo.log
-rw-r–r–. 1 root root 276279 Feb 17 00:45 dnf.log
-rw-r–r–. 1 root root 60566 Feb 17 00:45 dnf.rpm.log
-rw-r–r–. 1 root root 3859 Feb 17 00:45 hawkey.log
drwxr-sr-x+ 3 root systemd-journal 46 Feb 13 10:21 journal
-rw-rw-r–. 1 root utmp 292299 Feb 14 15:58 lastlog
-rw——- 1 root root 0 Feb 17 10:19 maillog
-rw——- 1 root root 0 Feb 13 10:49 maillog-20250217
-rw——- 1 root root 4624 Feb 17 10:19 messages
-rw——- 1 root root 3290421 Feb 17 10:11 messages-20250217
drwx——. 9 root root 6 Dec 19 16:08 private
-rw——- 1 root root 850 Feb 17 10:19 secure
-rw——- 1 root root 41075 Feb 17 10:11 secure-20250217
-rw——- 1 root root 0 Feb 17 10:19 spooler
-rw——- 1 root root 0 Feb 13 10:49 spooler-20250217
drwxrwx— 9 squid root 94 Feb 17 10:19 squid
-rw——-. 1 root root 0 Dec 19 16:08 tallylog
-rw-rw-r–. 1 root utmp 18439 Feb 14 15:58 wtmp
drwxr-xr-x 9 zabbix zabbix 65 Feb 17 10:19 zabbix

日付が入ったフィルが出来ており、問題なさそう。

念のため、タイマーのリストも再度確認。こちらも問題なく追加されていました。

$ sudo systemctl enable logrotate.timer
$
$ sudo systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
Mon 2025-02-17 10:15:26 JST 7s left Mon 2025-02-17 10:14:26 JST 52s ago refresh-policy-routes@enX0.timer refresh-policy-routes@enX0.service
Mon 2025-02-17 13:47:26 JST 2h 32min left Sun 2025-02-16 13:47:26 JST 21h ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2025-02-17 20:10:44 JST 8h left Mon 2025-02-17 00:45:26 JST 10h ago update-motd.timer update-motd.service
Tue 2025-02-18 00:00:00 JST 12h left Mon 2025-02-17 10:12:17 JST 3min 1s ago logrotate.timer logrotate.service
Mon 2025-02-24 01:15:09 JST 6 days left Mon 2025-02-17 00:43:26 JST 10h ago fstrim.timer fstrim.service

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

logrotateさせるファイルを複数書くと、そもそもlogrotateが動作しないんですね。有効なところまで動作してくれてもいいのにとは思いますが、重複しているどちらが優先かシステム的には分からないのでこういった動作になるんでしょうか。
何はともあれ、単純にヒューマンエラーなので、設定のチェックなどが重要ですね。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

This site uses Akismet to reduce spam. Learn how your comment data is processed.