利用logrotate进行切割
#创建切割配置
cat > /etc/logrotate.d/nginx <<EOF
/opt/nginx/logs/*.log {
daily
missingok
rotate 90
compress
delaycompress
notifempty
create 644 root root
sharedscripts
dateext
dateformat _%Y%m%d
postrotate
if [ -f /opt/nginx/logs/nginx.pid ]; then
kill -USR1 \`cat /opt/nginx/logs/nginx.pid\`
fi
endscript
}
EOF
#/opt/nginx/logs/*.log注意替换为实际路径
#/opt/nginx/logs/nginx.pid注意替换为实际路径
#rotate 90保留90天
创建定时任务
echo "0 0 * * * root bash /usr/sbin/logrotate -f /etc/logrotate.d/nginx" >> /etc/crontab