nginx日志切割脚本

[root@iZ25lr4xysvZ ~]# cat /opt/awweb/script/nginx_log_cut_v1.sh
#nginx日志切割脚本

#!/bin/bash
#设置日志文件存放目录
logs_path="/var/log/nginx/"
#设置pid文件
pid_path="/var/run/nginx.pid"
#设置hostname
#hostname="JK123-56-159-206"
#设置日志id
logid="1"

#重命名日志文件
mv ${logs_path}access.log ${logs_path}access_${hostname}_${logid}_$(date -d "yesterday" +"%Y%m%d").log
mv ${logs_path}error.log ${logs_path}error_${hostname}_${logid}_$(date -d "yesterday" +"%Y%m%d").log
#向nginx主进程发信号重新打开日志
kill -USR1 `cat ${pid_path}`

gzip ${logs_path}access_${hostname}_${logid}_$(date -d "yesterday" +"%Y%m%d").log
gzip ${logs_path}error_${hostname}_${logid}_$(date -d "yesterday" +"%Y%m%d").log
find ${logs_path} -maxdepth 1 -mtime +30 -name '*.gz' -exec rm -f {} \;
[root@elk-ctc-bj-10-254-64-45 nginxldap]# cat /opt/nginxldap/nginx_logrote.sh
gs_path="/data/logs/nginxldap/"
PIDFILE="/opt/nginxldap/logs/nginx.pid"
ACCESS_LOG="${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log"
ERROR_LOG="${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/error_$(date -d "yesterday" +"%Y%m%d").log"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
/bin/mv ${logs_path}access.log $ACCESS_LOG
#/bin/mv ${logs_path}error.log $ERROR_LOG
kill -USR1 `cat $PIDFILE`
/bin/gzip -9 $ACCESS_LOG
#/bin/gzip -9 $ERROR_LOG
find ${logs_path} -name "*.gz" -mtime +90|xargs rm -rf
文档更新时间: 2019-07-13 11:58   作者:月影鹏鹏