logrotate でログローテーションしましょう。
logrotate と cron を使って定期的にログローテーションしましょう。
logrotate
logrotate とcron がインストールされていることを確認します。
[root@centos7-101 ~]# yum list installed |grep logrotate logrotate.x86_64 3.8.6-17.el7 @base [root@centos7-101 ~]# yum list installed |grep cron cronie.x86_64 1.4.11-20.el7_6 @updates cronie-anacron.x86_64 1.4.11-20.el7_6 @updates crontabs.noarch 1.11-6.20121102git.el7 @anaconda
インストールされていない場合は、インストールします。
# yum install -y logrotate # yum install -y cron
cron が動作していることを確認します。
[root@centos7-101 ~]# systemctl status crond.service ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 土 2019-02-02 16:22:49 JST; 5min ago Main PID: 4938 (crond) CGroup: /system.slice/crond.service └─4938 /usr/sbin/crond -n 2月 02 16:22:49 centos7-101 systemd[1]: Started Command Scheduler. 2月 02 16:22:49 centos7-101 crond[4938]: (CRON) INFO (RANDOM_DELAY will be scaled with facto...d.) 2月 02 16:22:50 centos7-101 crond[4938]: (CRON) INFO (running with inotify support) 2月 02 16:22:50 centos7-101 crond[4938]: (CRON) INFO (@reboot jobs will be run at computer's...p.) Hint: Some lines were ellipsized, use -l to show in full.
開始していない場合は、開始します。
# systemctl start crond.service
動作確認用に /var/log/boot.log を /tmp/bootlog にコピーしました。
[root@centos7-101 ~]# ls /var/log anaconda chrony grubby_prune_debug messages-20190120 spooler-20190112 audit cron lastlog messages-20190127 spooler-20190120 boot.log cron-20181216 maillog rhsm spooler-20190127 boot.log-20181206 cron-20190112 maillog-20181216 secure tallylog boot.log-20181208 cron-20190120 maillog-20190112 secure-20181216 tuned boot.log-20181216 cron-20190127 maillog-20190120 secure-20190112 wtmp boot.log-20190112 dmesg maillog-20190127 secure-20190120 yum.log boot.log-20190120 dmesg.old messages secure-20190127 yum.log-20190112 btmp firewalld messages-20181216 spooler btmp-20190202 grubby messages-20190112 spooler-20181216 [root@centos7-101 ~]# mkdir /tmp/bootlog [root@centos7-101 ~]# cp /var/log/boot.* /tmp/bootlog/ [root@centos7-101 ~]# cd /tmp/bootlog/ [root@centos7-101 bootlog]# ls boot.log boot.log-20181208 boot.log-20190112 boot.log-20181206 boot.log-20181216 boot.log-20190120
動作確認用の設定ファイルを作成します。
[root@centos7-101 bootlog]# vi /tmp/bootlog/logrotate.conf /tmp/bootlog/boot.log { }
logrotate を実行してみます。
[root@centos7-101 bootlog]# /usr/sbin/logrotate -d /tmp/bootlog/logrotate.conf reading config file /tmp/bootlog/logrotate.conf Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /tmp/bootlog/boot.log 1048576 bytes (no old logs will be kept) empty log files are rotated, old logs are removed considering log /tmp/bootlog/boot.log log does not need rotating (log size is below the 'size' threshold) [root@centos7-101 bootlog]# ls -l 合計 96 -rw-------. 1 root root 0 2月 2 16:31 boot.log -rw-------. 1 root root 17924 2月 2 16:31 boot.log-20181206 -rw-------. 1 root root 16588 2月 2 16:31 boot.log-20181208 -rw-------. 1 root root 8338 2月 2 16:31 boot.log-20181216 -rw-------. 1 root root 25121 2月 2 16:31 boot.log-20190112 -rw-------. 1 root root 8384 2月 2 16:31 boot.log-20190120 -rw-r--r--. 1 root root 26 2月 2 16:43 logrotate.conf
この実行結果は、閾値を下回ったファイルしかないのでログローテーションは必要ないということですね。
確認していませんでしたが、boot.log は 0 byte です。
確認用のコピーデータなので、変更します。
[root@centos7-101 bootlog]# dd if=/dev/zero of=boot.log bs=512k count=2 2+0 レコード入力 2+0 レコード出力 1048576 バイト (1.0 MB) コピーされました、 0.00230426 秒、 455 MB/秒 [root@centos7-101 bootlog]# ls -la 合計 1108 drwxr-xr-x. 2 root root 144 2月 2 17:18 . drwxrwxrwt. 9 root root 187 2月 2 16:31 .. -rw-r--r--. 1 root root 1048576 2月 2 17:18 boot.log -rw-------. 1 root root 17924 2月 2 16:31 boot.log-20181206 -rw-------. 1 root root 16588 2月 2 16:31 boot.log-20181208 -rw-------. 1 root root 25121 2月 2 16:31 boot.log-20190112 -rw-------. 1 root root 8384 2月 2 16:31 boot.log-20190120 -rw-r--r--. 1 root root 26 2月 2 16:43 logrotate.conf root@centos7-101 bootlog]# /usr/sbin/logrotate -d /tmp/bootlog/logrotate.conf reading config file /tmp/bootlog/logrotate.conf Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /tmp/bootlog/boot.log 1048576 bytes (no old logs will be kept) empty log files are rotated, old logs are removed considering log /tmp/bootlog/boot.log log needs rotating rotating log /tmp/bootlog/boot.log, log->rotateCount is 0 dateext suffix '-20190202' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' renaming /tmp/bootlog/boot.log.1 to /tmp/bootlog/boot.log.2 (rotatecount 1, logstart 1, i 1), renaming /tmp/bootlog/boot.log.0 to /tmp/bootlog/boot.log.1 (rotatecount 1, logstart 1, i 0), fscreate context set to unconfined_u:object_r:user_tmp_t:s0 renaming /tmp/bootlog/boot.log to /tmp/bootlog/boot.log.1 disposeName will be /tmp/bootlog/boot.log.1 removing old log /tmp/bootlog/boot.log.1 error: error opening /tmp/bootlog/boot.log.1: そのようなファイルやディレクトリはありません
最終的に古いファイルを boot.log.1 を削除しようとした時に存在しないので、エラーになっています。
[root@centos7-101 bootlog]# vi logrotate.conf /tmp/bootlog/boot.log { missingok } [root@centos7-101 bootlog]# /usr/sbin/logrotate -d /tmp/bootlog/logrotate.conf reading config file /tmp/bootlog/logrotate.conf Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /tmp/bootlog/boot.log 1048576 bytes (no old logs will be kept) empty log files are rotated, old logs are removed considering log /tmp/bootlog/boot.log log needs rotating rotating log /tmp/bootlog/boot.log, log->rotateCount is 0 dateext suffix '-20190202' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' renaming /tmp/bootlog/boot.log.1 to /tmp/bootlog/boot.log.2 (rotatecount 1, logstart 1, i 1), renaming /tmp/bootlog/boot.log.0 to /tmp/bootlog/boot.log.1 (rotatecount 1, logstart 1, i 0), fscreate context set to unconfined_u:object_r:user_tmp_t:s0 renaming /tmp/bootlog/boot.log to /tmp/bootlog/boot.log.1 disposeName will be /tmp/bootlog/boot.log.1 removing old log /tmp/bootlog/boot.log.1 error: error opening /tmp/bootlog/boot.log.1: そのようなファイルやディレクトリはありません
missingok を追加することでファイルが無い場合には無視するようになると思ったのですが、エラーが無くならないないですね。
とりあえずファイルを作ってみます。
[root@centos7-101 bootlog]# touch boot.log.1 [root@centos7-101 bootlog]# ls -l 合計 1108 -rw-r--r--. 1 root root 1048576 2月 2 17:18 boot.log -rw-------. 1 root root 17924 2月 2 16:31 boot.log-20181206 -rw-------. 1 root root 16588 2月 2 16:31 boot.log-20181208 -rw-------. 1 root root 25121 2月 2 16:31 boot.log-20190112 -rw-------. 1 root root 8384 2月 2 16:31 boot.log-20190120 -rw-r--r--. 1 root root 0 2月 2 17:41 boot.log.1 -rw-r--r--. 1 root root 37 2月 2 17:40 logrotate.conf [root@centos7-101 bootlog]# /usr/sbin/logrotate -d /tmp/bootlog/logrotate.conf reading config file /tmp/bootlog/logrotate.conf Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /tmp/bootlog/boot.log 1048576 bytes (no old logs will be kept) empty log files are rotated, old logs are removed considering log /tmp/bootlog/boot.log log needs rotating rotating log /tmp/bootlog/boot.log, log->rotateCount is 0 dateext suffix '-20190202' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' renaming /tmp/bootlog/boot.log.1 to /tmp/bootlog/boot.log.2 (rotatecount 1, logstart 1, i 1), renaming /tmp/bootlog/boot.log.0 to /tmp/bootlog/boot.log.1 (rotatecount 1, logstart 1, i 0), fscreate context set to unconfined_u:object_r:user_tmp_t:s0 renaming /tmp/bootlog/boot.log to /tmp/bootlog/boot.log.1 disposeName will be /tmp/bootlog/boot.log.1 removing old log /tmp/bootlog/boot.log.1
エラーはなくなりました。
古いファイルを圧縮する設定を追加してみます。
[root@centos7-101 bootlog]# vi logrotate.conf /tmp/bootlog/boot.log { missingok compress } [root@centos7-101 bootlog]# /usr/sbin/logrotate -d /tmp/bootlog/logrotate.conf reading config file /tmp/bootlog/logrotate.conf Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /tmp/bootlog/boot.log 1048576 bytes (no old logs will be kept) empty log files are rotated, old logs are removed considering log /tmp/bootlog/boot.log log needs rotating rotating log /tmp/bootlog/boot.log, log->rotateCount is 0 dateext suffix '-20190202' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' renaming /tmp/bootlog/boot.log.1.gz to /tmp/bootlog/boot.log.2.gz (rotatecount 1, logstart 1, i 1), renaming /tmp/bootlog/boot.log.0.gz to /tmp/bootlog/boot.log.1.gz (rotatecount 1, logstart 1, i 0), fscreate context set to unconfined_u:object_r:user_tmp_t:s0 renaming /tmp/bootlog/boot.log to /tmp/bootlog/boot.log.1 disposeName will be /tmp/bootlog/boot.log.1.gz compressing log with: /bin/gzip removing old log /tmp/bootlog/boot.log.1.gz error: error opening /tmp/bootlog/boot.log.1.gz: そのようなファイルやディレクトリはありません
ファイルが無いというエラーになりますが、拡張子.gz が付いているので、圧縮は有効なようです。
しかしファイルを消してしまっていて、ローテーションというよりも削除になっています。
[root@centos7-101 bootlog]# ls -l 合計 84 -rw-------. 1 root root 17924 2月 2 16:31 boot.log-20181206 -rw-------. 1 root root 16588 2月 2 16:31 boot.log-20181208 -rw-------. 1 root root 25121 2月 2 16:31 boot.log-20190112 -rw-------. 1 root root 8384 2月 2 16:31 boot.log-20190120 -rw-r--r--. 1 root root 46 2月 2 17:45 logrotate.conf
世代管理してみます。
[root@centos7-101 bootlog]# vi logrotate.conf /tmp/bootlog/boot.log { missingok compress rotate 1 } エラーになるであろうファイルを作成しておきます。 [root@centos7-101 bootlog]# touch boot.log.2.gz [root@centos7-101 bootlog]# ls -l 合計 1108 -rw-r--r--. 1 root root 1048576 2月 2 17:58 boot.log -rw-------. 1 root root 17924 2月 2 16:31 boot.log-20181206 -rw-------. 1 root root 16588 2月 2 16:31 boot.log-20181208 -rw-------. 1 root root 25121 2月 2 16:31 boot.log-20190112 -rw-------. 1 root root 8384 2月 2 16:31 boot.log-20190120 -rw-r--r--. 1 root root 0 2月 2 18:03 boot.log.2.gz -rw-r--r--. 1 root root 56 2月 2 18:00 logrotate.conf root@centos7-101 bootlog]# /usr/sbin/logrotate -d /tmp/bootlog/logrotate.conf reading config file /tmp/bootlog/logrotate.conf Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /tmp/bootlog/boot.log 1048576 bytes (1 rotations) empty log files are rotated, old logs are removed considering log /tmp/bootlog/boot.log log needs rotating rotating log /tmp/bootlog/boot.log, log->rotateCount is 1 dateext suffix '-20190202' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' renaming /tmp/bootlog/boot.log.1.gz to /tmp/bootlog/boot.log.2.gz (rotatecount 1, logstart 1, i 1), renaming /tmp/bootlog/boot.log.0.gz to /tmp/bootlog/boot.log.1.gz (rotatecount 1, logstart 1, i 0), fscreate context set to unconfined_u:object_r:user_tmp_t:s0 renaming /tmp/bootlog/boot.log to /tmp/bootlog/boot.log.1 compressing log with: /bin/gzip removing old log /tmp/bootlog/boot.log.2.gz
実際に動かしてみます。
[root@centos7-101 bootlog]# /usr/sbin/logrotate /tmp/bootlog/logrotate.conf [root@centos7-101 bootlog]# ls -l 合計 88 -rw-------. 1 root root 17924 2月 2 16:31 boot.log-20181206 -rw-------. 1 root root 16588 2月 2 16:31 boot.log-20181208 -rw-------. 1 root root 25121 2月 2 16:31 boot.log-20190112 -rw-------. 1 root root 8384 2月 2 16:31 boot.log-20190120 -rw-r--r--. 1 root root 1051 2月 2 17:58 boot.log.1.gz -rw-r--r--. 1 root root 54 2月 2 18:08 logrotate.conf
boot.log.1.gz が作成されて boot.log と boot.log.2.gz が存在しないので、成功したようです。
もう一度、実行してみます。今度はboot.log を 2M にしました。
[root@centos7-101 bootlog]# dd if=/dev/zero of=boot.log bs=1024k count=2 2+0 レコード入力 2+0 レコード出力 2097152 バイト (2.1 MB) コピーされました、 0.00359489 秒、 583 MB/秒 [root@centos7-101 bootlog]# ls -l 合計 2136 -rw-r--r--. 1 root root 2097152 2月 2 18:12 boot.log -rw-------. 1 root root 17924 2月 2 16:31 boot.log-20181206 -rw-------. 1 root root 16588 2月 2 16:31 boot.log-20181208 -rw-------. 1 root root 25121 2月 2 16:31 boot.log-20190112 -rw-------. 1 root root 8384 2月 2 16:31 boot.log-20190120 -rw-r--r--. 1 root root 1051 2月 2 17:58 boot.log.1.gz -rw-r--r--. 1 root root 54 2月 2 18:08 logrotate.conf [root@centos7-101 bootlog]# /usr/sbin/logrotate -f /tmp/bootlog/logrotate.conf [root@centos7-101 bootlog]# ls -l 合計 88 -rw-------. 1 root root 17924 2月 2 16:31 boot.log-20181206 -rw-------. 1 root root 16588 2月 2 16:31 boot.log-20181208 -rw-------. 1 root root 25121 2月 2 16:31 boot.log-20190112 -rw-------. 1 root root 8384 2月 2 16:31 boot.log-20190120 -rw-r--r--. 1 root root 2067 2月 2 18:12 boot.log.1.gz -rw-r--r--. 1 root root 54 2月 2 18:08 logrotate.conf
boot.log.1.gz のサイズが変わっているので、とりあえずは OK でしょう。
ここまでは動作確認用の設定ファイルを使ってきましたが、本来の設定ファイルに追加してみます。
設定ファイルは /etc/logrotate.d/以下に設定ファイルを追加します。
[root@centos7-101 ~]# ls -l /etc/logrotate.d/ 合計 20 -rw-r--r--. 1 root root 91 4月 11 2018 bootlog -rw-r--r--. 1 root root 160 9月 15 2017 chrony -rw-r--r--. 1 root root 224 10月 30 23:49 syslog -rw-r--r--. 1 root root 100 10月 31 08:03 wpa_supplicant -rw-r--r--. 1 root root 103 11月 5 10:53 yum [root@centos7-101 ~]# mv /tmp/bootlog/logrotate.conf /etc/logrotate.d/test.conf [root@centos7-101 ~]# ls -l /etc/logrotate.d/ 合計 24 -rw-r--r--. 1 root root 91 4月 11 2018 bootlog -rw-r--r--. 1 root root 160 9月 15 2017 chrony -rw-r--r--. 1 root root 224 10月 30 23:49 syslog -rw-r--r--. 1 root root 54 2月 2 18:08 test.conf -rw-r--r--. 1 root root 100 10月 31 08:03 wpa_supplicant -rw-r--r--. 1 root root 103 11月 5 10:53 yum
動作確認用に作成した設定ファイルを /etc/logrotate.d/ 以下に移動(コピーでも)します。
[root@centos7-101 ~]# mv /tmp/bootlog/logrotate.conf /etc/logrotate.d/test.conf [root@centos7-101 ~]# ls -l /etc/logrotate.d/ 合計 24 -rw-r--r--. 1 root root 91 4月 11 2018 bootlog -rw-r--r--. 1 root root 160 9月 15 2017 chrony -rw-r--r--. 1 root root 224 10月 30 23:49 syslog -rw-r--r--. 1 root root 54 2月 2 18:08 test.conf -rw-r--r--. 1 root root 100 10月 31 08:03 wpa_supplicant -rw-r--r--. 1 root root 103 11月 5 10:53 yum [root@centos7-101 ~]# dd if=/dev/zero of=/tmp/bootlog/boot.log bs=512k count=2 2+0 レコード入力 2+0 レコード出力 1048576 バイト (1.0 MB) コピーされました、 0.00241084 秒、 435 MB/秒 [root@centos7-101 ~]# ls -l /tmp/bootlog/ 合計 1108 -rw-r--r--. 1 root root 1048576 2月 2 18:22 boot.log -rw-------. 1 root root 17924 2月 2 16:31 boot.log-20181206 -rw-------. 1 root root 16588 2月 2 16:31 boot.log-20181208 -rw-------. 1 root root 25121 2月 2 16:31 boot.log-20190112 -rw-------. 1 root root 8384 2月 2 16:31 boot.log-20190120 -rw-r--r--. 1 root root 2067 2月 2 18:12 boot.log.1.gz [root@centos7-101 ~]# logrotate -dv /etc/logrotate.conf 必要なところだけ抜粋 rotating pattern: /tmp/bootlog/boot.log weekly (1 rotations) empty log files are rotated, old logs are removed considering log /tmp/bootlog/boot.log log does not need rotating (log has been already rotated)
logrotate.conf 自体の設定もありますが、とりあえずは動いています。
実際に動作を確認したいので、ローテーション情報を変更してみます。
[root@centos7-101 ~]# cat /var/lib/logrotate/logrotate.status logrotate state -- version 2 "/var/log/yum.log" 2019-1-12-16:10:2 "/tmp/bootlog/boot.log" 2019-2-2-18:13:4 "/var/log/boot.log" 2019-1-20-15:46:4 "/var/log/wtmp" 2018-11-11-19:0:0 "/var/log/chrony/*.log" 2018-11-11-19:0:0 "/var/log/spooler" 2019-1-27-17:22:2 "/var/log/btmp" 2019-2-2-15:44:2 "/var/log/maillog" 2019-1-27-17:22:2 "/var/log/wpa_supplicant.log" 2018-11-11-19:0:0 "/var/log/secure" 2019-1-27-17:22:2 "/var/log/messages" 2019-1-27-17:22:2 "/var/log/cron" 2019-1-27-17:22:2 [root@centos7-101 ~]# vi /var/lib/logrotate/logrotate.status [root@centos7-101 ~]# cat /var/lib/logrotate/logrotate.status logrotate state -- version 2 "/var/log/yum.log" 2019-1-12-16:10:2 "/tmp/bootlog/boot.log" 2019-1-1-18:13:4 "/var/log/boot.log" 2019-1-20-15:46:4 "/var/log/wtmp" 2018-11-11-19:0:0 "/var/log/chrony/*.log" 2018-11-11-19:0:0 "/var/log/spooler" 2019-1-27-17:22:2 "/var/log/btmp" 2019-2-2-15:44:2 "/var/log/maillog" 2019-1-27-17:22:2 "/var/log/wpa_supplicant.log" 2018-11-11-19:0:0 "/var/log/secure" 2019-1-27-17:22:2 "/var/log/messages" 2019-1-27-17:22:2 "/var/log/cron" 2019-1-27-17:22:2 [root@centos7-101 ~]# logrotate -dv /etc/logrotate.conf 必要なところだけ抜粋 rotating pattern: /tmp/bootlog/boot.log weekly (1 rotations) empty log files are rotated, old logs are removed considering log /tmp/bootlog/boot.log log needs rotating rotating log /tmp/bootlog/boot.log, log->rotateCount is 1 dateext suffix '-20190202' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' glob finding old rotated logs failed fscreate context set to unconfined_u:object_r:user_tmp_t:s0 renaming /tmp/bootlog/boot.log to /tmp/bootlog/boot.log-20190202 creating new /tmp/bootlog/boot.log mode = 0644 uid = 0 gid = 0 compressing log with: /bin/gzip
実際にローテーションしてみます。
[root@centos7-101 ~]# logrotate -f /etc/logrotate.conf [root@centos7-101 ~]# ls -l /tmp/bootlog/ 合計 88 -rw-r--r--. 1 root root 0 2月 2 18:42 boot.log -rw-------. 1 root root 17924 2月 2 16:31 boot.log-20181206 -rw-------. 1 root root 16588 2月 2 16:31 boot.log-20181208 -rw-------. 1 root root 25121 2月 2 16:31 boot.log-20190112 -rw-------. 1 root root 8384 2月 2 16:31 boot.log-20190120 -rw-r--r--. 1 root root 1051 2月 2 18:22 boot.log-20190202.gz -rw-r--r--. 1 root root 2067 2月 2 18:12 boot.log.1.gz
boot.log-20190202.gz がローテーションしたファイルで、boot.log がサイズ 0 になっています。
[root@centos7-101 ~]# cat /var/lib/logrotate/logrotate.status logrotate state -- version 2 "/var/log/yum.log" 2019-2-2-18:42:19 "/var/log/boot.log" 2019-1-20-15:46:4 "/tmp/bootlog/boot.log" 2019-2-2-18:42:19 "/var/log/chrony/*.log" 2018-11-11-19:0:0 "/var/log/wtmp" 2019-2-2-18:42:19 "/var/log/spooler" 2019-2-2-18:42:19 "/var/log/btmp" 2019-2-2-18:42:19 "/var/log/maillog" 2019-2-2-18:42:19 "/var/log/wpa_supplicant.log" 2018-11-11-19:0:0 "/var/log/secure" 2019-2-2-18:42:19 "/var/log/messages" 2019-2-2-18:42:19 "/var/log/cron" 2019-2-2-18:42:19
次に、ログローテーションを cron で動作するように設定しますが、長くなってしまったため、今回はここまでさせていただきます。