一、背景:
我们的nginx日志文件之前没有做归档保存,导致日益增大,以后会越来越大直到打不开,因此我们希望实现自定义归档保存,比如:按天切割日志归档保存,最多保存30天的日志文件。
二、logrotate实现全流程记录:
(1)检查是否安装了logrotate,默认系统自带:
执行命令:logrotate --help
(2)在任意你想存放的目录下新建一个配置文件,比如我直接放在了nginx的日志目录logs下: /app/nginx/nginx/logs/nginx_log_split.conf
# 需要注意,下面内容中的路径需要根据实际进行调整
/app/nginx/nginx/logs/*.log {
daily
dateext
missingok
rotate 30
compress
delaycompress
notifempty
create 0644 root root
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
[ -s /app/nginx/nginx/logs/nginx.pid ] && kill -USR1 `cat /app/nginx/