使用logrotate和acrontab设置nginx日志切割

第一部分:修改nginx,指向新地址

[root@host-172-22-14-89 data]# mkdir /data/log/nginx -p

 

[root@host-172-22-14-89 log]# vim /etc/nginx/nginx.conf

error_log /var/log/nginx/error.log;

access_log  /var/log/nginx/access.log  main;

 

修改为

error_log /data/log/nginx/error.log;

access_log  /data/log/nginx/access.log  main;

 

[root@host-172-22-14-89 log]# service nginx restart

[root@host-172-22-14-89 log]# ls nginx/

access.log  error.log

 

 

 

 

第二部分:修改acrontab配置,设置切割时间

[root@host-172-22-14-89 ~]# vim /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=45

# the jobs will be started during the following hours only

START_HOURS_RANGE=3-22

 

#period in days   delay in minutes   job-identifier   command

1       5       cron.daily              nice run-parts /etc/cron.daily

7       25      cron.weekly             nice run-parts /etc/cron.weekly

@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

 

 

修改为:

START_HOURS_RANGE=0-01

重启服务

[root@host-172-22-14-89 ~]# service crond restart

 

第三部分:修改logrotate日志

[root@host-172-22-14-89 log]# cd /etc/logrotate.d/

[root@host-172-22-14-89 logrotate.d]# cp nginx nginx_new

[root@host-172-22-14-89 logrotate.d]# vim nginx_new

/var/log/nginx/*log {

    create 0644 nginx nginx

    daily

    rotate 10

    missingok

    notifempty

    compress

    sharedscripts

    postrotate

        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true

    endscript

}

 

修改为:

/data/log/nginx/*log {

    create 0644 nginx nginx

    daily

    rotate 180

    missingok

    notifempty

    compress

    sharedscripts

    postrotate

        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true

    endscript

}

修改配置文件后,并不需要重启服务

 

配置文件

执行文件:/usr/sbin/logrotate

主配置文件:/etc/logrotate.conf

自定义配置文件:/etc/logrotate.d/*.conf

你可能感兴趣的:(Linux)