logrotate 是一款強大的日誌文件管理工具,能自動分割、壓縮、刪除舊日誌並創建新日誌,有效節省磁盤空間。本文詳細介紹其在 CentOS 系統上的配置與使用方法。
logrotate [OPTION...] <configfile>
常用選項:
預設配置文件位於 /etc/logrotate.conf,核心參數說明:
針對 LNMP 一鍵安裝包中的 Nginx 訪問日誌(位於 /home/wwwlogs),創建專用配置:
vim /etc/logrotate.d/nginx
配置內容:
/home/wwwlogs/access.log /home/wwwlogs/nginx_error.log {
notifempty
daily
rotate 5
sharedscripts
postrotate
/bin/kill -HUP `/bin/cat /usr/local/nginx/logs/nginx.pid`
endscript
}
關鍵指令解析:
手動測試:
/usr/sbin/logrotate -vf /etc/logrotate.conf
自動執行機制:系統預置 /etc/cron.daily/logrotate 腳本,由 cron 每日自動調用,無需額外配置。