首页 > 代码库 > nginx日志切割shell脚本

nginx日志切割shell脚本

#!/bin/bash
#function:cut nginx log files shell
#cp /usr/local/nginx/logs/access.log /usr/local/nginx/logs/access.log.bak
log_cut_path="/var/log/nginx"
log_files_path="/usr/local/nginx/logs/"
log_files_dir=${log_cut_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
log_files_name=(access)
nginx_pid=`ps aux | grep ‘nginx: master‘ | grep -v ‘grep nginx‘ | awk ‘{print $2}‘`
save_days=90
mkdir -p $log_cut_path
log_files_num=${#log_files_name[@]}
#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
kill -USR1 $nginx_pid
find $log_files_path -mtime +$save_days -exec rm -rf {} \;

现在的nginx日志存放目录 log_files_path="/usr/local/nginx/logs/"

切割后的日志存放目录 log_cut_path="/var/log/nginx"

第三行第一次运行一下

加入crontab

0 0 * * * sh /root/nginxlogcut.sh


参考 http://sndapk.blog.51cto.com/5385144/1297120 有改动

本文出自 “一颗石头 BBOTTE的博客” 博客,请务必保留此出处http://bbotte.blog.51cto.com/6205307/1411870