使用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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值