質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
logrotate

logrotateはログの世代管理やサイズ制限を行うLinux標準のツールです。

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

Q&A

解決済

1回答

3733閲覧

logrotateでローテーション条件になっても古いログへ書き込み続ける場合の対処法を教えてください(copytruncateなしで)

halohalolin

総合スコア46

logrotate

logrotateはログの世代管理やサイズ制限を行うLinux標準のツールです。

Linux

Linuxは、Unixをベースにして開発されたオペレーティングシステムです。日本では「リナックス」と呼ばれています。 主にWebサーバやDNSサーバ、イントラネットなどのサーバ用OSとして利用されています。 上位500のスーパーコンピュータの90%以上はLinuxを使用しています。 携帯端末用のプラットフォームAndroidは、Linuxカーネル上に構築されています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

0グッド

0クリップ

投稿2022/11/11 03:17

編集2022/11/11 06:45

前提

Amazon Linux 2においてlogrotateでローテーション条件になっても古いログへ書き込み続ける場合の対処法をcopytruncateなしで教えてください。

現在対処中のシステムについて、ローテーションの時期になってlogrotateはファイル操作をするのですが、なぜかログ出力するシステムは移動されたファイルへ書き込み続けるのでログローテートの意味がなく困っています。

なお対処法としてcopytruncateは無しでお願いします。
copytruncateを使えば想定通りシステムは正しくログ出力し、ファイル肥大化しないことは確認しています。
しかし何としてもすべてのログを残す必要があるので、NGといたします。

※logrotateファイルの中身は同じなのに別のアカウントIDはどこか構成違いが影響しているのか、正常にlogrotateをしていて悩ましいです。

実現したいこと

以下のようにログ出力してほしい。

access.log <-ここにログを書き込む ...(略)... access.log-20221103 access.log-20221110

発生している問題

※肥大化するファイルはシステムの再起動タイミングなどで異なる。

access.log <-ファイルサイズが0 ...(略)... access.log-20221103 <-ファイルサイズが肥大化 access.log-20221110 <-ファイルサイズが0

該当のソースコード

/etc/logrotate.conf

logrotate

1# see "man logrotate" for details 2# rotate log files weekly 3weekly 4 5# keep 4 weeks worth of backlogs 6rotate 4 7 8# create new (empty) log files after rotating old ones 9create 10 11# use date as a suffix of the rotated file 12#dateext 13 14# uncomment this if you want your log files compressed 15#compress 16 17# packages drop log rotation information into this directory 18include /etc/logrotate.d 19 20# system-specific logs may be also be configured here.

cat /etc/logrotate.d/apache2

logrotate

1/var/log/apache2/*.log { 2 daily 3 missingok 4 rotate 14 5 compress 6 delaycompress 7 notifempty 8 create 640 root adm 9 sharedscripts 10 postrotate 11 if invoke-rc.d apache2 status > /dev/null 2>&1; then \ 12 invoke-rc.d apache2 reload > /dev/null 2>&1; \ 13 fi; 14 endscript 15 prerotate 16 if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ 17 run-parts /etc/logrotate.d/httpd-prerotate; \ 18 fi; \ 19 endscript 20}

デバッグモードでの実行結果

text

1# logrotate -d /etc/logrotate.conf 2WARNING: logrotate in debug mode does nothing except printing debug messages! Consider using verbose mode (-v) instead if this is not what you want. 3 4reading config file /etc/logrotate.conf 5including /etc/logrotate.d 6reading config file alternatives 7reading config file apache2 8reading config file apt 9reading config file btmp 10reading config file dpkg 11reading config file rsyslog 12reading config file wtmp 13Reading state from file: /var/lib/logrotate/status 14Allocating hash table for state file, size 64 entries 15 16Handling 9 logs 17 18rotating pattern: /var/log/alternatives.log monthly (12 rotations) 19empty log files are not rotated, old logs are removed 20considering log /var/log/alternatives.log 21 log /var/log/alternatives.log does not exist -- skipping 22Creating new state 23 24rotating pattern: /var/log/apache2/*.log after 1 days (14 rotations) 25empty log files are not rotated, old logs are removed 26considering log /var/log/apache2/access.log 27Creating new state 28 Now: 2022-11-11 06:28 29 Last rotated at 2022-11-11 06:00 30 log does not need rotating (log has been already rotated) 31considering log /var/log/apache2/error.log 32Creating new state 33 Now: 2022-11-11 06:28 34 Last rotated at 2022-11-11 06:00 35 log does not need rotating (log has been already rotated) 36considering log /var/log/apache2/other_vhosts_access.log 37Creating new state 38 Now: 2022-11-11 06:28 39 Last rotated at 2022-11-11 06:00 40 log does not need rotating (log has been already rotated) 41not running prerotate script, since no logs will be rotated 42not running postrotate script, since no logs were rotated 43 44rotating pattern: /var/log/apt/term.log monthly (12 rotations) 45empty log files are not rotated, old logs are removed 46considering log /var/log/apt/term.log 47Creating new state 48 Now: 2022-11-11 06:28 49 Last rotated at 2022-11-11 06:00 50 log does not need rotating (log has been already rotated) 51 52rotating pattern: /var/log/apt/history.log monthly (12 rotations) 53empty log files are not rotated, old logs are removed 54considering log /var/log/apt/history.log 55Creating new state 56 Now: 2022-11-11 06:28 57 Last rotated at 2022-11-11 06:00 58 log does not need rotating (log has been already rotated) 59 60rotating pattern: /var/log/btmp monthly (1 rotations) 61empty log files are rotated, old logs are removed 62considering log /var/log/btmp 63 log /var/log/btmp does not exist -- skipping 64Creating new state 65 66rotating pattern: /var/log/dpkg.log monthly (12 rotations) 67empty log files are not rotated, old logs are removed 68considering log /var/log/dpkg.log 69Creating new state 70 Now: 2022-11-11 06:28 71 Last rotated at 2022-11-11 06:00 72 log does not need rotating (log has been already rotated) 73 74rotating pattern: /var/log/syslog 75 after 1 days (7 rotations) 76empty log files are not rotated, old logs are removed 77considering log /var/log/syslog 78Creating new state 79 Now: 2022-11-11 06:28 80 Last rotated at 2022-11-11 06:00 81 log does not need rotating (log has been already rotated) 82 83rotating pattern: /var/log/mail.info 84/var/log/mail.warn 85/var/log/mail.err 86/var/log/mail.log 87/var/log/daemon.log 88/var/log/kern.log 89/var/log/auth.log 90/var/log/user.log 91/var/log/lpr.log 92/var/log/cron.log 93/var/log/debug 94/var/log/messages 95 weekly (4 rotations) 96empty log files are not rotated, old logs are removed 97considering log /var/log/mail.info 98Creating new state 99 Now: 2022-11-11 06:28 100 Last rotated at 2022-11-11 06:00 101 log does not need rotating (log has been already rotated) 102considering log /var/log/mail.warn 103Creating new state 104 Now: 2022-11-11 06:28 105 Last rotated at 2022-11-11 06:00 106 log does not need rotating (log has been already rotated) 107considering log /var/log/mail.err 108Creating new state 109 Now: 2022-11-11 06:28 110 Last rotated at 2022-11-11 06:00 111 log does not need rotating (log has been already rotated) 112considering log /var/log/mail.log 113Creating new state 114 Now: 2022-11-11 06:28 115 Last rotated at 2022-11-11 06:00 116 log does not need rotating (log has been already rotated) 117considering log /var/log/daemon.log 118 log /var/log/daemon.log does not exist -- skipping 119Creating new state 120considering log /var/log/kern.log 121 log /var/log/kern.log does not exist -- skipping 122Creating new state 123considering log /var/log/auth.log 124Creating new state 125 Now: 2022-11-11 06:28 126 Last rotated at 2022-11-11 06:00 127 log does not need rotating (log has been already rotated) 128considering log /var/log/user.log 129Creating new state 130 Now: 2022-11-11 06:28 131 Last rotated at 2022-11-11 06:00 132 log does not need rotating (log has been already rotated) 133considering log /var/log/lpr.log 134 log /var/log/lpr.log does not exist -- skipping 135Creating new state 136considering log /var/log/cron.log 137 log /var/log/cron.log does not exist -- skipping 138Creating new state 139considering log /var/log/debug 140 log /var/log/debug does not exist -- skipping 141Creating new state 142considering log /var/log/messages 143Creating new state 144 Now: 2022-11-11 06:28 145 Last rotated at 2022-11-11 06:00 146 log does not need rotating (log has been already rotated) 147not running postrotate script, since no logs were rotated 148 149rotating pattern: /var/log/wtmp monthly (1 rotations) 150empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed 151considering log /var/log/wtmp 152 log /var/log/wtmp does not exist -- skipping 153Creating new state

※/var/lib/logrotate/logrotate.status も調べようとしましたが、/var/lib/logrotate内が空フォルダだったので、他の場所に格納していないか調査中です

以上よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

novelistory

2022/11/11 05:08

nginxで同様の事象が起きている方がいました。ご参考までに。 https://teratail.com/questions/90582 箇条書きになりますがご容赦ください。 以下情報があれば回答を得られやすくなるかなと思いました。 ・上記URLに記載ありますが、デバックモードで実行(実際にログローテートはされません)し、その結果を記載する ・/etc/logrotate.confの中身(記載があるのは/etc/logrotate.d/配下のファイルかなと思いました) ・/var/lib/logrotate/logrotate.status ・タグに「Linux」とか「Amazon Linux 2」を追加する(logrotateをMyタグに設定している方はあまりいないかなあと思いました)
halohalolin

2022/11/11 06:20

novelistoryさんありがとうございます 私の環境はApache/2.4.38 (Debian)ですが、参考になるかどうかリンク先を確認してみます またその他ご指摘も反映します!
halohalolin

2022/11/11 06:23

タグ「Amazon Linux 2」はteratailで未登録のようなので、「Linux」のみ登録してみます
yukky1201

2022/11/11 07:41

------------------- prerotate if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ run-parts /etc/logrotate.d/httpd-prerotate; \ fi; \ endscript ------------------- 上記に記載の、httpd-prerotateはどのような処理をしているのでしょうか。 また、/etc/logrotate.confに「include /etc/logrotate.d」が記載されているため、二重に処理されていたりしませんか。
halohalolin

2022/11/20 23:21

yukky1201さん返答ありがとうございます。 今回の場合、docker内のログを調べていたのですが、/etc/logrotate.confをいじっていたので無駄に悩んでいたようです。 docker内のdocker外にログを出して外部からlogrotateをすると動きそうと気が付いたのでそちらで対応中です。
guest

回答1

0

ベストアンサー

logrotateは、ファイルをリネームして、新規にファイルを作るだけなので、何もしないと対象プロセスはそのまま古い方のリネームされたファイルに書き続けます。
切り替えるためには、その後、対象プロセス側で古いファイルをクローズして新規ログファイルをオープンするという動作が必要です。
postrotateの中で、kill -HUP $(cat 「apacheのPIDが入っているファイル」)とか、「apacheの実行ファイル」 reloadとかする必要があります。

postrotateの所になんか書いてありますが、そのファイルを調べてみましょう。

投稿2022/11/11 16:11

otn

総合スコア84499

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

halohalolin

2022/11/20 23:19

otnさんありがとうございます 今回の場合、docker内のログを調べていたのですが、docker外にログを出して外部からlogrotateをすると動きそうと気が付いたのでそちらで対応中です 今後もかねてベストアンサーとさせていただきました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問