创建脚本nginx_logs.sh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/bin/bash # 分割nginx access日志脚本 time=`date +%Y%m%d` logs_path="/usr/local/nginx/logs/" pid_path="/usr/local/nginx/logs/nginx.pid" #日志分割,按天创建文件夹 cd $logs_path website=`ls $logs_path/access_* | xargs -n 1 | cut -f 2 -d "_"` for i in $website do mkdir -p $logs_path/backup/$time/ mv $logs_path/access_$i $logs_path/backup/$time/$i done #向nginx主进程发信号重新打开日志 kill -USR1 `cat ${pid_path}` #每月1日删除所有超过30天日志。 if [ "`date +%d`" = "01" ]; then all_list=`ls $logs_path/backup | xargs -n 1` for del in $all_list do let results=$time-$del if [ $results -gt 30 ]; then rm -rf $logs_path/backup/$del fi done fi |
注: /usr/local/nginx/logs/下的日志格式为access_domain1.log。
加入计划任务,每天大概0时分割日志:
1 |
58 23 * * * /data/script/nginx_logs.sh |