#!/bin/bash
#此脚本用于自动分割Nginx的日志,包括access.log和error.log
#每天00:00执行此脚本 将前一天的access.log重命名为access-xxxx-xx-xx.log格式,并重新打开日志文件
#Nginx日志文件所在目录
LOG_PATH=/data/services/logs/Jarvis/
#获取昨天的日期
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
#获取pid文件路径
PID=/data/services/run/nginx.pid
#分割日志
for logfile in Api_access Bus_access;
do
ls -l ${LOG_PATH}${logfile}.log
mv ${LOG_PATH}${logfile}.log ${LOG_PATH}${logfile}.log-${YESTERDAY}.log
tar -zcvf ${LOG_PATH}${logfile}.log-${YESTERDAY}.log.tar.gz ${LOG_PATH}${logfile}.log-${YESTERDAY}.log --remove-files;
done
#mv ${LOG_PATH}nginx_error.log ${LOG_PATH}nginx_error-${YESTERDAY}.log
#向Nginx主进程发送USR1信号,重新打开日志文件
kill -USR1 `cat ${PID}`
chown nginx.nginx /data/services/logs -R