nginx每天自动分割并清除日志文件

Nginx产生的日志都是存在一个文件,网站运行时间越长,日志文件的大小也在不断增长,这对分析当天日志非常的不方便,所以需要每天把日志文件分割出来,并以时间命名,产生的日志无限增长,终有一天会导致磁盘爆满,而影。

创建日志分割脚本,保存为cut_logs.sh

#!/bin/bash
#function:cut nginx log files

#set the path to nginx log files
log_files_path="/home/nocome.com/logs/"
log_files_dir=${log_files_path}
#set nginx log files you want to cut
log_files_name=(access )
#set the path to nginx.
nginx_sbin="/usr/local/nginx/sbin/nginx"
#Set how long you want to save
save_days=30
############################################
#Please do not modify the following script #
############################################
#mkdir -p l o g f i l e s d i r l o g f i l e s n u m = log_files_dir log_files_num= logfilesdirlogfilesnum={#log_files_name[@]}

#cut nginx log files
for((i=0;i<$log_files_num;i++));do
mv l o g f i l e s p a t h {log_files_path} logfilespath{log_files_name[i]}.log l o g f i l e s d i r {log_files_dir} logfilesdir{log_files_name[i]}.log_$(date -d “yesterday” +"%Y-%m-%d")
done

#delete 30 days ago nginx log files
find l o g f i l e s p a t h − m t i m e + log_files_path -mtime + logfilespathmtime+save_days -exec rm -rf {} ;

#restart nginx
$nginx_sbin -s reload
复制代码

输入crontab -e命令,添加如下代码,每天0点00分启动。

00 00 * * * /nocome/cut_logs.sh

转自:http://www.nocome.com/thread-32636-1-2.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值