日志轮询设置linux,Linux中Logrotate轮询切割日志教程(Nginx)

很多软件在运行时都会产生随机日志,这些日志如果不经常清理会导致程序文件日积月累非常大,导致程序往大文件中写入日志变得很困难,打开文件也非常困难,所以我们有必要进行日志文件的切割,有利于实际操作中阅读日志文件,查看问题!

以下为切割Nginx运行日志教程,利用Linux系统自带Logrotate工具进行切割!

Logrotate的介绍

显而易见,Logrotate是基于CRON来运行的,其脚本是「/etc/cron.daily/logrotate」:

#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.conf

EXITVALUE=$?

if [ $EXITVALUE != 0 ]; then

/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"

fi

exit 0

实际运行时,Logrotate会调用配置文件「/etc/logrotate.conf」:

# see "man logrotate" for details

# rotate log files weekly

weekly

# keep 4 weeks worth of backlogs

rotate 4

# create new (empty) log files after rotating old ones

create

# uncomment this if you want your log files compressed

#compress

# RPM packages drop log rotation information into this directory

include /etc/logrotate.d

# no packages own wtmp -- we'll rotate them here

/var/log/wtmp {

monthly

minsize 1M

create 0664 root utmp

rotate 1

}

# system-specific logs may be also be configured here.

这里的设置可以理解为Logrotate的缺省值,当然了,可以我们在「/etc/logrotate.d」目录里放置自己的配置文件,用来覆盖Logrotate的缺省值。

Logrotate的演示

按天保存一周的Nginx日志压缩文件,配置文件为「/etc/logrotate.d/nginx」:

/usr/local/nginx/logs/*.log {

daily

dateext

compress

rotate 7

sharedscripts

postrotate

kill -USR1 `cat /var/run/nginx.pid`

endscript

}

如果你等不及CRON,可以通过如下命令来手动执行:

shell> logrotate -f /etc/logrotate.d/nginx

当然,正式执行前最好通过Debug选项来验证一下,这对调试也很重要:

shell> logrotate -d -f /etc/logrotate.d/nginx

(资源库 www.zyku.net)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值