nginx日志切割/分割,按天生成&定期删除日志-系统自带工具logrorate

 1.自设脚本切割

#!/bin/bash

LOG_PATH=/usr/local/nginx/logs
YESTERDAY=`date -d yesterday +%Y-%m-%d`
PID_PATH=/usr/local/nginx/logs/nginx.pid
mv $LOG_PATH/access.log $LOG_PATH/access-$YESTERDAY.log
[ ! -f $PID_PATH ] || kill -USR1 `cat $PID_PATH`
find $LOG_PATH/* -name "*.log" -type f -mtime +7 -exec rm -rf {} \;

添加定时任务

00 00 * * * /bin/sh  /usr/local/nginx/logs/log.sh
1、做一个日志分割脚本文件:
vi /data/svr/nginx/sbin/nginx_cutlog.sh
#!/bin/bash
#nginx日志分割脚本:
#crontab,需要root下设置: 0 0 * * * /data/svr/nginx/sbin/nginx_cutlog.sh
#设置日志文件存放目录
LOG_HOME="/data/svr/nginx/logs/"
#备分文件名称
LOG_PATH_BAK="$(date -d yesterday +%Y%m%d)"
#重命名日志文件
mv ${LOG_HOME}/access.log L O G H O M E / a c c e s s . {LOG_HOME}/access.LOG 
H
​
 OME/access.{LOG_PATH_BAK}.log
mv ${LOG_HOME}/error.log L O G H O M E / e r r o r . {LOG_HOME}/error.LOG 
H
​
 OME/error.{LOG_PATH_BAK}.log
#向nginx主进程发信号重新打开日志
kill -USR1 cat ${LOG_HOME}/nginx.pid

#删除90天前的日志
#find /data/svr/nginx/logs/ -atime +90 -exec rm -f {} ;

2、在root下新建个crontab定时任务:
crontab -e
#nginx日志分割
0 0 * * * /data/svr/nginx/sbin/nginx_cutlog.sh
#!/bin/bash
DATA=$(date -d "-1 day" "+%Y%m%d")
LOG_PATH="/usr/local/nginx/logs"
PID_PATH="/usr/local/nginx/logs/nginx.pid"
if [ -f $PID_PATH ]; then
        mv $LOG_PATH/access.log $LOG_PATH/access.$DATA.log
        mv $LOG_PATH/error.log $LOG_PATH/error.$DATA.log
        kill -USR1 $(cat $PID_PATH)
        find $LOG_PATH/* -name "*.log" -type f -mtime +15 | xargs rm -f
else
    echo "Error,Nginx is not working!"  >> /var/log/messages
fi

2.logrotate系统工具

yum -y install logrotate anacron

linux自带logrotate工具

主流的linux系统版本,都默认安装logrotate包,作为分割日志的系统工具,可以方便将日志按周期(日,周,月)和大小进行分割,其核心配置文件如下:

/etc/logrotate.conf   #核心配置
/etc/logrotate.d/     #存储自定义配置目录
/etc/anacrontab       #定时任务执行配置文件

 1)自定义nginx日志切割规则

mkdir /etc/logrotate.d.0/

vim /etc/logrotate.d.0/nginx

/var/log/nginx/*.log {
        create 0664 root root    #切割后创建日志文件的权限
        daily                     #指定转储周期为每天
        rotate 15                 #保留日志的天数
        missingok                 #忽略错误
        notifempty                #空文件不滚动切割
        compress                  #通过gzip 压缩转储以后的日志 
        delaycompress             #转储的日志文件到下一次转储时才压缩
        dateext                   #日志压缩文件以当天日期做后缀
        sharedscripts             #运行postrotate脚本
        postrotate                #给NGINX发信号要求重新生成日志
	     /bin/kill -USR1 `cat /home/nginx/logs/nginx.pid 2>/dev/null` || true   #刷新日志文件
        endscript
}

 2)然后在/etc/cron.daily/下增加配置logrotate

#!/bin/sh

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

!注意要是创建的nginx日志切割规则如果在/etc/logrotate.d/下面创建nginx文件定时是在凌晨三点多切断。所以我自定义到/etc/logrotate.d.0/nginx ,这个目录就是不受/etc/anacrontab文件控制

配置文件为: /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=9 (这里是个最大随机延时时间)
# the jobs will be started during the following hours only
START_HOURS_RANGE=23-24(这是日志切割时间点,即只在23点到24点开始切割,不出意外就是23点啦)
#period in days delay in minutes job-identifier command

3)配置定时任务定时切割日志

crontab -e
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d.0/nginx >/dev/null 2>&1

 附


logrotate 的配置文件是 /etc/logrotate.conf。主要参数如下表:
compress 通过gzip 压缩转储以后的日志
nocompress 不需要压缩时,用这个参数
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断
nocopytruncate 备份日志文件但是不截断
create mode owner group 转储文件,使用指定的文件模式创建新的日志文件
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty 如果是空文件的话,不转储
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月

4)yum源下载的nginx默认配置

vim /etc/logrotate.d/nginx

/var/log/nginx/*log {
    create 0664 root root
    daily
    rotate 10
    missingok
    notifempty
    dateext
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}

会在晚上三点多自动切割日志

你可能感兴趣的:(linux,基础知识,nginx,nginx,服务器,运维)