依照此篇作法:Apache Log檔 使用日期格式 當 檔名設定,客製化 Log 檔名,卻會造成檔名砍不乾淨的問題,要怎麼正確清掉過期得檔案呢?
依照時間來刪除非固定檔名的 Apache2 Log 檔
logrotate 測試方式
- /usr/sbin/logrotate -vdf /etc/logrotate.conf # 測試跑得狀況
主要指令
- find . -mtime +30 -name "access.*.log" -print
- find . -mtime +30 -name "access.*.log" -exec rm {} \; -print
- 重點 rotate 0, find ...
參考範例
/logs/system/mindundi/* { rotate 0 daily missingok sharedscripts postrotate find /logs/system/mindundi/ -name "*log" -mtime +15 -exec /bin/rm -f {} \; endscript }
/tmp/logrotate/dummy { firstaction find /logs/xxx//xxx -name ".log" -mtime +7 -exec /bin/rm -f {} \; find /logs/yyy//www* -name "www.log" -mtime +7 -exec /bin/rm -f {} \; find /logs/zzz//qqq* -name ".log" -mtime +7 -exec /bin/rm -f {} \; endscript rotate 1 daily missingok }
/var/log/rsync/dummy { daily rotate 0 create ifempty lastaction /usr/bin/find /var/log/rsync/ -mtime +7 -delete /usr/bin/find /var/log/rsync/ -mtime +1 -exec gzip -q {} \; endscript }
我自己的設定作法如下:
/var/log/apache2/*.log { daily rotate 30 compress notifempty create 644 root adm sharedscripts postrotate if /etc/init.d/apache2 status > /dev/null ; then \ /etc/init.d/apache2 reload > /dev/null; \ fi; /usr/bin/find /var/log/apache2/ -mtime +30 -delete endscript }