Nginx日志分割并自动删除日志

#!/bin/bash
# 分割nginx日志脚本
 
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

你可能感兴趣的:(Nginx,shell)