|
由于公司网站很多,要是手动做nginx日志切割就会很繁杂,不如弄些shell脚本实现半自动处理。 首先,统一设置各站点的日志文件格式
# vi domain.sh
#!/bin/sh
sed -i '/access_log/d' /usr/local/nginx/vhost/*.conf
#### change path to /usr/local/nginx/vhost and use the command to get the DOMAIN_LIST :
### ls -lh *.conf |awk '{print $9}'|sed 's/$/ \/' > domain.txt ####
DOMAIN_LIST=(
bbs.elecfans.com.conf
cdnimg.xianbey.com.conf
c.elecfans.com.conf
......
common.qbaobei.com.conf
elecfans.com.conf
)
DOMAIN_ALL_LEN=${#DOMAIN_LIST}
i=0
while [ $i -lt $DOMAIN_ALL_LEN ]
do
sed -i "/server_name/ a access_log logs/access.${DOMAIN_LIST[$i]}.log main;" /usr/local/nginx/vhost/${DOMAIN_LIST[$i]}
let i++
done
接着测试下nginx的配置有没错误 # /etc/init.d/nginx test ## 如果没有错误就reload下nginx,有错那就检查配置吧 ## # /etc/init.d/nginx reload 按日期轮循切割nginx日志
[root@]# vi Cut_nginx_error_log.sh
#!/bin/sh
## Cut Nginx Error Log ##
LOGS_PATH=/usr/local/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${LOGS_PATH}/error.log ${LOGS_PATH}/error_${YESTERDAY}.log
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
[root@]# vi Cut_nginx_log.sh
#!/bin/sh
## Cut Nginx Log ##
### use the follow command to get th ACCESS_LOG_LIST:
### sed 's/conf/&.log/' domain.txt > log.txt
### sed 's/^/access.&/' log.txt >> ng_log.txt ###
LOGS_PATH=/usr/local/nginx/logs
YESTERDAY=`date -d "yesterday" +%Y-%m-%d`
ACCESS_LOG_LIST=(
access.log
access.bbs.elecfans.com.conf.log
access.cdnimg.xianbey.com.conf.log
......
access.c.elecfans.com.conf.log
access.common.qbaobei.com.conf.log
access.elecfans.com.conf.log
)
ACCESS_LOG_ALL_LEN=${#ACCESS_LOG_LIST}
i=0
while [ $i -lt $ACCESS_LOG_ALL_LEN ]
do
mv ${LOGS_PATH}/${ACCESS_LOG_LIST[$i]} ${LOGS_PATH}/${ACCESS_LOG_LIST[$i]}.${YESTERDAY}.log
let i++
done
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
删除日志
[root@]# vi Del_nginx_log.sh
#!/bin/sh
#Delete Nginx Log
LOGS_PATH=/usr/local/nginx/logs
YESTERDAY=$(date -d "-3day" +%Y-%m-%d)
rm -rf ${LOGS_PATH}/access*.conf.log.${YESTERDAY}.log
rm -rf ${LOGS_PATH}/error_${YESTERDAY}.log
设置crontab定时任务 [root@]# crontab -e 00 00 * * * /bin/bash /usr/local/nginx/sbin/Cut_nginx_log.sh 00 00 * * * /bin/bash /usr/local/nginx/sbin/Cut_nginx_error_log.sh 00 18 * * * /bin/bash /usr/local/nginx/sbin/Del_nginx_log.sh 如此便可
PS:有不当之处望请各位友友指点 |
