1. Background
The system checks /data/nginx/log
the size of the log file at 1:59 in the morning every day . If it is less than 10M, no processing is done. If it is greater than or equal to 10M, a log dump is triggered.
After the dump / data / nginx / log size of 0, and will /data/nginx/logs/
generate a gzip compressed by the current date in the named file .gz
Note: This configuration cannot be regarded as log cutting, because when the log file is 25M, it will be archived as a file. It is not divided in units of 10M and can only be called log dump.
2. Log cutting configuration file /etc/logrotate.conf
cat > /etc/logrotate.conf << EOF
/data/nginx/log {
missingok
notifempty
create 0640 root root
compress
dateext
olddir /data/nginx/logs/
rotate 50
size 10M
}
EOF
Related explanation:
/data/nginx/log #表示要做切割的日志文件名
missingok #在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误
notifempty #如果日志文件为空,轮循不会进行
create 0640 root root #以指定的权限创建全新的日志文件
compress #在轮循任务完成后,使用gzip压缩归档文件
dateext #使用当期日期作为归档文件的命名格式
olddir /data/nginx/logs/ #指定归档文件存放到/data/nginx/logs/目录下,/data/nginx/logs/需提前创建
rotate 50 #最多存储50个归档文件,当生成第51个归档文件时,第1个会被删除
size 10M #当监听文件达到10M时进行切割归档
Three, set up timing tasks
echo "59 01 * * * /usr/sbin/logrotate -v /etc/logrotate.conf &> /var/log/logrotate.log" >> /var/spool/cron/root
crond start
Reference document: https://www.cnblogs.com/gaoyuechen/p/7731973.html