且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

nginx日志按天切割shell

更新时间:2022-09-16 16:54:11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
##############################################
#Author: Liuzhengwei - 1135960569@qq.com
#QQ:1135960569
#Last modified: 2017-08-16 16:05
#Filename: nginx_logrotate.sh
#Description:
##############################################
back_dir=/data/nginx_log
year=`date '+%Y'`
month=`date '+%m'`
if [ ! -d $back_dir/$year ];then
        mkdir $back_dir/$year
        if [ ! -d $back_dir/$year/$month ];then
                mkdir $back_dir/$year/$month
        fi
fi
cp $back_dir/access.log $back_dir/$year/$month
cp $back_dir/error.log $back_dir/$year/$month
cd $back_dir/$year/$month
gzip -c access.log > access.log_`(date '+%F')`.gz
gzip -c error.log > error.log_`(date '+%F')`.gz
find ./ -maxdepth 1 -name "*.log" xargs rm -f
>$back_dir/access.log
>$back_dir/error.log

任务计划:

1
59 23 * * * /server/scripts/nginx_logrotate.sh &> /dev/null

本文转自激情燃烧的岁月博客51CTO博客,原文链接http://blog.51cto.com/liuzhengwei521/1967719如需转载请自行联系原作者

weilovepan520