nginx分割日志,多日志循环处理

原来网上也有很多nginx分割日志的方法,发现都是针对access和error两个日志,今天完善一下,多项目多日志循环处理。

vim /data/script/cut_nginx_log.sh

#!/bin/bash

# This script run at 00:00

DATA_TIME=$(date +%F-%H:%M:%S)

# The Nginx logs path

nginx_log="/data/logs/nginx"

# Backup logs path

logs_path="/data/logs/cut_nginx_logs"

pid=$(cat /data/server/nginx/nginx.pid)

[ -d $logs_path ] || mkdir -p $logs_path

for f in $(find $nginx_log/ -type f);

do

echo ${f} > /tmp/tmp_log_name.txt

mv ${f} ${logs_path}/$(awk -F"/" '{print $NF}' $(echo /tmp/tmp_log_name.txt))_${DATA_TIME}.log;

done

kill -USR1 ${pid}

然后加入计划任务

crontab -e

00 00 * bash /data/script/cut_nginx_log.sh

相关推荐