I wrote an article about shell script splitting Nginx logs and cleaning them regularly, link: http://www.webyang.net/Html/web/article_255.html but need to reload nginx, some people have a psychological shadow on restarting nginx , so I changed it again:
- #!/bin/bash
- #nginx access log segmentation shell script
- #log directory
- log_dir="/usr/local/nginx/logs/"
- cd $ log_dir
- time=`date +%Y%m%d --date="-1 day"`
- #Hisashi preservation heaven number
- save_days=15
- #The position of the log file name is divided by '/', key('/' number + 1)
- num = 6 ;
- #To be optimized here (get the file name without a suffix)
- website=`ls $log_dir*.log | xargs -n 1 | cut -f 1 -d "." | cut -f $num -d "/"`
- #All log files in the log directory are folders, and the corresponding time files will be created cyclically
- for i in $website
- do
- #Check if the directory exists
- if[!-d $log_dir$i ];then
- mkdir "$i"
- be
- cp $log_dir$i.log $log_dir$i/$i-$time.log
- true>$log_dir$i.log
- done
- find $log_dir -mtime +$save_days -exec rm -rf {} \;
Set up crontab to run once every morning.
展示: web1.log => web1/web1-20160322.log web1/web1-20160323.log web2.log => web2/web2-20160322.log web2/web2-20160323.log
There are other methods such as cronolog, logrotate, etc., you can refer to: http://www.tuicool.com/articles/BF36rq