搭建openresty的web服务之后,日志文件随着时间和业务量会逐渐增大,既占用磁盘空间,又不便于日志查看,调查问题。这个时候,就可以使用logrotate做下定时日志切割啦,既方便又省事。
一、logrotate介绍
logrotate程序是一个日志文件管理工具。用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到“转储”作用。可以节省磁盘空间。Linux系统默认安装logrotate工具,它默认的配置文件在:/etc/logrotate.conf ,/etc/logrotate.d/,Logrotate是基于CRON来运行的,其脚本是/etc/cron.daily/logrotate,日志轮转是系统自动完成的。实际运行时,Logrotate会调用配置文件/etc/logrotate.conf。
(1)关于CRON服务的定时周期执行时间,centos系统请查看/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=3-22 表示作业的执行时间范围3点-22点之间执行,基本就是3点开始的,再就是延时。
RANDOM_DELAY=45,这个是随机最大延时时间,具体还是要看分任务的延时,比如daily的delay in minutes配置是5,那就是至少延时5分钟了,也就是每日定时任务执行时间会是在3:05之后。
二、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
# use date as a suffix of the rotated file
dateext
# 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 and btmp -- we'll rotate them here
/var/log/wtmp {

本文介绍了如何使用logrotate工具对OpenResty产生的日志进行定期切割,以节省磁盘空间并简化问题排查。详细讲解了logrotate的基本概念、配置参数、CRON定时执行机制,以及在openresty中的定制配置实例。
&spm=1001.2101.3001.5002&articleId=126479111&d=1&t=3&u=2b3a9aa5ccda4933b0d0ccada0d280b8)
1520

被折叠的 条评论
为什么被折叠?



