此文档只针对linux分割nginx日志
nginx日志不自动带日志分割功能,需要自己写脚本分割日志,并加入到每日任务中,定期分割、存储、删除日志
此脚本包括,日志分割、保存、删除、重建日志文件
#!/bin/bash #date +%Y%m%d%H%M%S YEAR=`date +%Y` MONTH=`date +%m` DAY=`date +%d` HOUR=`date +%H` MINUTE=`date +%M` SECOND=`date +%S` #pid path pid_path="/portal/software/nginx/logs/nginx.pid" #LOG path LOGDIR=/portal/software/nginx/logs/back_up_log/ LOGDIR2=/portal/software/nginx/logs/sotp_error_log/ # Screening log files LOGNAME=`ls /portal/software/nginx/logs/ |grep "\.log"` #Create the LOGDIR #mkdir -p $LOGDIR1 #mkdir -p $LOGDIR2 #Move the log file to new position, and change the name for i in ${LOGNAME} do #LOG name LOGFILE=${i}-$YEAR"-"$MONTH"-"$DAY"-"$HOUR$MINUTE$SECOND mv -f /portal/software/nginx/logs/$i ${LOGDIR}/${LOGFILE} && touch /portal/software/nginx/logs/$i done # reload nginx process kill -USR1 `cat ${pid_path}` # remove logs 6 days ago /usr/bin/find $LOGDIR -name "*" -mtime +6 | /usr/bin/xargs rm -f
加入日常任务
crontab -e
00 01 * * * /bin/sh /opt/cronjob/mysql_daily_backup.sh
注: 前面五项,分别是 分、时、日、月、周 上面意思是每天的晚上一点执行日志分割脚本
保存后,重启crond任务
/etc/init.d/crond restart