#!/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 有改动
nginx日志切割shell脚本,布布扣,bubuko.com
时间: 2024-10-12 13:13:23