在公司服务器上的Nginx日志是每天切割一次,随着访问量的上升,每天产生的日志体积也随之变大。
正好最近要开发一个对Nginx日志分析的工具,借此机会把公司服务器上Nginx日志的切割时间间隔调整为一个小时,用于切割日志的Shell脚本如下:
代码语言:javascript复制#!/bin/bash
# Every hour running time.
# by Jason.wei.
# The Nginx logs path
log_year=$(date "%Y")
log_month=$(date "%m")
log_day=$(date "%d")
log_hour=$(date "%H")
logs_path="/usr/local/nginx/logs/"
save_path="/usr/local/nginx/logs/$log_year-$log_month/$log_day/"
if [ ! -d "$save_path" ]; then
mkdir -p "$save_path"
fi
mv ${logs_path}access.log ${save_path}access_${log_year}${log_month}${log_day}_${log_hour}.log
if [ $log_hour = 00 ]; then
mv ${logs_path}error.log ${save_path}error_$log_year$log_month$log_day.log
fi
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`