#!/bin/bash
# This script run at 00:00
## 零点执行该脚本
## Nginx 日志文件所在的目录
LOGS_PATH=/u01/apps/nginx/logs
LOGS_LIST=(`ls $LOGS_PATH/*access.log`)
## 获取昨天的 yyyy-MM-dd
YESTERDAY=`date -d "yesterday" +%Y-%m-%d`
## 移动文件
for logs in ${LOGS_LIST[@]};do
varlog=`basename $logs`
mv -f $LOGS_PATH/$varlog $LOGS_PATH/$varlog.$YESTERDAY
done
## 向Nginx主进程发送USR1信号.USR1信号是重新打开日志文件
kill -USR1 $(cat $LOGS_PATH/nginx.pid)
或者:
#!/bin/bash
# 这个脚本须在每天的00:00运行
# nginx日志文件的存放位置
logs_path="/data1/logs"
mkdir -p ${logs_path}${date -d "yesterday" +"%Y"}/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}${date -d "yesterday" +"%Y"}/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/nginx.pid`
cronteb -e
00 00 * * * /bin/bash xxx.sh