按日自动切割nginx日志,并删除30(可自定义)天以上的日志。
脚本保存为 /nginx/logs/ngx_logcut.sh 并加入crontab中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#!/bin/bash # 0 00 * * * /nginx/logs/ngx_logcut.sh >/dev/null 2>& pidfile=/var/run/nginx.pid logpath='/nginx/logs/' keepdays=30 logfiles=(access.log error.log) cd $logpath for logfile in ${logfiles[@]}; do if [ ! -e $logfile ]; then continue fi find . -type f -name $logfile"20*" -mtime +$keepdays -exec rm {} \; mv $logfile $logfile$(date -d "yesterday" +"%Y%m%d") done kill -USR1 `cat $pidfile` |