最新消息:

nginx 分割日志

nginx admin 2934浏览 0评论

nginx的access.log总是记录很多东西,一两天就变成几十M,如果长时间下去,日志文件会很大很大。所以要分割日志:

vim /wp-content/local/nginx/sbin/cut_nginx_log.sh

输入

#!/bin/bash

# This script run at 00:00

# The Nginx logs path

logs_path="/home/vhosts/"

# Nginx logs names here

logs_names=(default phpmyadmin)

#mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/

num=${#logs_names[@]}

for((i=0;i<num;i++));do

mv ${logs_path}${logs_names[i]}/logs/access.log ${logs_path}${logs_names[i]}/logs/$(date -d "yesterday" +"%Y%m%d")_access.log

old=${logs_path}${logs_names[i]}/logs/$(date -d "8 days ago" +"%Y%m%d")_access.log

if [ -f ${old} ]; then

    rm ${old};

fi

done

kill -USR1 `cat /wp-content/local/nginx/logs/nginx.pid`

然后赋予其权限:

chmod a+x /wp-content/local/nginx/sbin/cut_nginx_log.sh

加入到crontab计划任务:

crontab -u root -e

0 0 * * * /wp-content/local/nginx/sbin/cut_nginx_log.sh

CTRL+X保存文件。

有些vps需要重启contab:

/etc/init.d/cron restart

这样 就行了。

转载请注明:爱开源 » nginx 分割日志

您必须 登录 才能发表评论!