Nginx日志切割脚本(按天切割)

Nginx日志切割脚本(按天切割)

让日志按天存放 每天凌晨00:00把前一天的Nginx日志access.log重命名为access-xxxx-xx-xx.log格式 例如:access-2018-5-20.log

[root@localhost ~]# cat /data/scripts/auto_cuts_nginx_log.sh

#!/usr/bin/env bash
# 2018-5-20 21:24:20
# Automatic Cutting of Nginx logs
nginx=(
    [0]=access.log
    [1]=/usr/local/nginx
    [2]=access_`date +%Y%m%d`.log
    [3]=error.log
    [4]=error_`date +%Y%m%d`.log
    [5]=`ps -ef |grep nginx|awk '/master/ {print $2}'`
)
#分割日志
mv ${nginx[1]}/logs/${nginx[0]} ${nginx[1]}/logs/${nginx[2]}
mv ${nginx[1]}/logs/${nginx[3]} ${nginx[1]}/logs/${nginx[4]}
#kill -USR1 PID向Nginx主进程发送USR1信号。USR1信号是重新打开日志文件
kill -USR1 ${nginx[5]}
if [ $? -eq 0 ]
then
echo -e "\033[32m The ${nginx[2]} Cutting Successfully \033[0m"
echo -e "\033[32m The ${nginx[4]} Cutting Successfully \033[0m"
fi

Last updated