Nginx日志切割

0点定时切割日志(安全审计一般需要至少保留半年的日志)

0 0 * * * /usr/bin/bash /app/sh/cut_nginx_log.sh

脚本:

#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="/data/nginx/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
mv ${logs_path}error.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/error_$(date -d "yesterday" +"%Y%m%d").log

kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

gzip ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
gzip ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/error_$(date -d "yesterday" +"%Y%m%d").log

 

posted @ 2020-08-25 15:33  luchuangao  阅读(174)  评论(0编辑  收藏  举报