这是一段Linux的Shell脚本,用于切割Nginx的日志,可通过crontab设置每日零点执行这段脚本实现自动切割。
代码语言:javascript复制#!/bin/bash
# This script run at 00:00
log_year=$(date -d "yesterday" "%Y")
log_month=$(date -d "yesterday" "%m")
log_day=$(date -d "yesterday" "%d")
# The Nginx logs path
logs_path="/usr/local/nginx/logs/"
save_path="/usr/local/nginx/logs/$log_year-$log_month/"
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
mv ${logs_path}error.log ${save_path}error_$log_year$log_month$log_day.log
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
阳光部落原创,更多内容请访问 http://www.sunbloger.com/