debian nginx日志切割脚本 - 忆未来

debian nginx日志切割脚本

1.创建脚本 /root/cut_nginx_log.sh
输入以下内容

#!/bin/bash
#自动版日志切割 无需设置
#function:cut nginx log files for lnmp v0.5 and v0.6
#author: http://lnmp.org

#set the path to nginx log files
log_files_path="/home/wwwlogs/"
log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
#set nginx log files you want to cut
#设置要切割的日志的名字,如果日志目录下面的日志文件名为nginx_access.log,则填写nginx_access,每个日志名用空格分隔
log_files_name=( nginx_error nginx_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 $log_files_dir

log_files_num=${#log_files_name[@]}
#日志文件将会存放到/home/wwwlogs/年/月/日志文件名_年月日.log
#cut nginx log files
for((i=0;i<$log_files_num;i++));do
mv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").log
done

#delete 30 days ago nginx log files
find $log_files_path -mtime +$save_days -exec rm -rf {} \; 

$nginx_sbin -s reload

  1. 给脚本添加可执行权限:
[root@localhost scripts]# chmod a+x /root/cut_nginx_log.sh

3.设置crontab,每天凌晨00:00切割nginx访问日志

# crontab -e

输入以下内容:

00 00 * * * /root/cut_nginx_log.sh

标签: none

评论已关闭