linux logrotate介绍

logrotate是Linux系统内置的日志转储工具,用于避免日志文件过大,节省存储空间。它通过配置文件(如/etc/logrotate.conf和/etc/logrotate.d/*)进行定制,并与crond服务配合工作,按设定的周期执行。执行流程涉及anacron和多个cron级别的任务,确保日志的有效管理和清理。
摘要由CSDN通过智能技术生成

我们知道linux系统日志文件位于/var/log目录下,在/var/log目录下,我们会发现有相当一部分日志文件末尾包含一串数字(E.g:ecure-20231114),这表明这部分日志文件经过了日志转储,以避免日志文件过大,导致可读性较差。

[root@rhel77 log]# pwd
/var/log
[root@rhel77 log]# ls -l secure*
-rw------- 1 root root  1792 Nov 14 13:49 secure
-rw------- 1 root root 78478 Nov  8 14:50 secure-20231108
-rw------- 1 root root  3011 Nov 10 21:37 secure-20231110
-rw------- 1 root root  5421 Nov 13 14:31 secure-20231113
-rw------- 1 root root  3886 Nov 14 10:02 secure-20231114
[root@rhel77 log]# 

说起上述日志转储,很多人会选择使用crontab定时执行shell脚本进行日志转储,但是在linux上内置了日志转储工具,它就是logrotate,且其在linux上默认是安装了的。

[root@rhel77 ~]# rpm -qa | grep logrotate
logrotate-3.8.6-17.el7.x86_64
[root@rhel77 ~]# 

1.logrotate介绍

logrotate是linux自带工具,通过简单配置,用来对日志进行转储和定时清除,从而避免单个日志文件过大,以及众多的日志文件占用存储空间。

2.logrotate配置讲解

logrotate是基于crond服务来实时运行的,其crond服务的脚本默认是/etc/cron.daily/logroate,日志存储时系统自动完成的。logrotate的配置文件为/etc/logrotate.conf(主配置)和/etc/logrotate.d/*(子配置),当logrotate实际运行时,其会调用主配置文件/etc/logrotate.conf,如果在/etc/logrotate.d目录里也自定义了配置文件,即:当主配置和子配置有冲突时,以子配置文件为准。

3.logrotate执行流程

当crond启动后,crond服务加载/etc/cron.d/0hourly-->在每小时的01分执行/etc/cron.hourly/0anacron-->执行anacron-->根据/etc/cron.daily/logrotate获得上次运行logrotate的时间(查看/var/lib/logrotate/logrotate.status文件)-->执行/etc/cron.hourly及根据/etc/anacrontab的配置执行/etc/cron.daily,/etc/cron.weekly,/etc/cron.monthly-->执行/etc/cron.daily下的logrotate脚本-->执行logrotate-->根据/etc/logrotate.conf配置或/etc/logrotate.d目录下的配置执行xx应用-->成功转储xx日志。

-->/etc/cron.d/0hourly

[root@rhel77 cron.d]# cat /etc/cron.d/0hourly 
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly
[root@rhel77 cron.d]# 

-->/etc/cron.hourly/0anacron 

[root@rhel77 cron.d]# cat /etc/cron.hourly/0anacron 
#!/bin/sh
# Check whether 0anacron was run today already
if test -r /var/spool/anacron/cron.daily; then
    day=`cat /var/spool/anacron/cron.daily`
fi
if [ `date +%Y%m%d` = "$day" ]; then
    exit 0;
fi

# Do not run jobs when on battery power
if test -x /usr/bin/on_ac_power; then
    /usr/bin/on_ac_power >/dev/null 2>&1
    if test $? -eq 1; then
    exit 0
    fi
fi
/usr/sbin/anacron -s
[root@rhel77 cron.d]# 

-->/var/spool/anacron

cron.daily

[root@rhel77 anacron]# cat cron.daily 
20231117

cron.weekly

[root@rhel77 anacron]# cat cron.weekly 
20231110

cron.monthly

[root@rhel77 anacron]# cat cron.monthly 
20231019

-->/etc/cron.daily/logrotate

[root@rhel77 cron.daily]# cat 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
[root@rhel77 cron.daily]# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑要上天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值