logrotate日志管理工具与split文件切割命令
概述
logrotate是一个Linux系统默认安装了的日志文件管理工具,用来把旧文件轮转、压缩、删除,并且创建新的日志文件。我们可以根据日志文件的大小、天数等来转储,便于对日志文件管理。
logrotate是基于crond服务来运行的,其crond服务的脚本是/etc/cron.daily/logrotate,日志转储是系统自动完成的。实际运行时,logrotate会调用配置文件 /etc/logrotate.conf,可以在 /etc/logrotate.d 目录里放置自定义好的配置文件,用来覆盖logrotate的缺省值。
配置文件详解
/etc/logrotate.conf 是主配置文件
/etc/logrotate.d 是一个目录,该目录下的所有文件都会被主动的读到 /etc/logrotate.conf 中执行
配置项 | 说明 |
compress | 通过 gzip 压缩转储旧的日志 |
nocompress | 不需要压缩时,用这个参数 |
copytruncate | 用于还在打开中的日志文件,把当前日志备份并截断,是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据 |
nocopytruncate | 备份日志文件但是不截断 |
create mode owner group | 使用指定的文件模式创建新的日志文件,如:create 0664 root utmp |
nocreate | 不建立新的日志文件 |
delaycompress | 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩 |
nodelaycompress | 覆盖 delaycompress 选项,转储同时压缩 |
missingok | 在日志转储期间,任何错误将被忽略 |
errors address | 转储时的错误信息发送到指定的 Email 地址 |
ifempty | 即使日志文件是空文件也转储,这个是 logrotate 的缺省选项 |
notifempty | 如果日志文件是空文件的话,不转储 |
mail E-mail | 把转储的日志文件发送到指定的 E-mail 地址 |
nomail | 转储时不发送日志文件到 E-mail 地址 |
olddir directory | 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统 |
noolddir | 转储后的日志文件和当前日志文件放在同一个目录下 |
prerotate/endscript | 在转储之前需要执行的命令可以放入这个对中,这两个关键字必须单独成行 |
postrotate/endscript | 在转储之后需要执行的命令可以放入这个对中,这两个关键字必须单独成行 |
sharedscripts | 所有的日志文件都转储完毕后统一执行一次脚本 |
daily | 指定转储周期为每天 |
weekly | 指定转储周期为每周 |
monthly | 指定转储周期为每月 |
rotate count | 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5个备份 |
size(minsize) logsize | 当日志文件到达指定的大小时才转储,size 可以指定单位为k或M,如:size 500k,size 100M |
dateext | 指定转储后的日志文件以当前日期为格式结尾, |
dateformat dateformat | 配合dateext使用,紧跟在下一行出现,定义日期格式,只支持%Y %m %d %s这4个参数,如:dateformat -%Y%m%d%s |
软件信息说明、
[root@master log]# rpm -ql logrotate /etc/cron.daily/logrotate /etc/logrotate.conf # 主配置文件
/etc/logrotate.d # 配置目录
测试logrotate如何管理日志
这里我们将创建一个10MB的日志文件/var/log/log-file。我们将展示怎样使用logrotate来管理该日志文件。
我们从创建一个日志文件开始吧,然后在其中填入一个10MB的随机比特流数据文件。
[root@master log]# touch log-file
[root@master log]# head -c 10M < /dev/urandom > /var/log/log-file
现在日志文件已经准备好,我们将配置logrotate来轮循该日志文件。让我们为该文件创建一个配置文件。
[root@master logrotate.d]# cat log-file /var/log/log-file { monthly 指定转储周期为每月 rotate 5 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5个备份 compress 通过 gzip 压缩转储旧的日志 delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩 missingok 在日志转储期间,任何错误将被忽略 notifempty 如果日志文件是空文件的话,不转储
dateext 指定转储后的日志文件以当前日期为格式结尾, create 644 root root 使用指定的文件模式创建新的日志文件 postrotate /usr/bin/killall -HUP rsyslogd 在转储之后需要执行的命令可以放入这个对中,这两个关键字必须单独成行 endscript }
[root@master log]# logrotate -vf /etc/logrotate.d/log-file [root@master log]# ls -lh log-file* -rw-r--r-- 1 root root 0 11月 29 15:35 log-file -rw-r--r-- 1 root root 10M 11月 29 15:35 log-file-20191129
即使轮循条件没有满足,我们也可以通过使用‘-f’选项来强制logrotate轮循日志文件,‘-v’参数提供了详细的输出。
Logrotate的记录日志
logrotate自身的日志通常存放于/var/lib/logrotate/status目录。如果处于排障目的,我们想要logrotate记录到任何指定的文件,我们可以指定像下面这样从命令行指定。
[root@master log]# head -c 10M < /dev/urandom > /var/log/log-file [root@master log]# logrotate -vf -s /var/log/logrotate-status /etc/logrotate.d/log-file reading config file /etc/logrotate.d/log-file Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /var/log/log-file forced from command line (5 rotations) empty log files are not rotated, old logs are removed considering log /var/log/log-file log needs rotating rotating log /var/log/log-file, log->rotateCount is 5 dateext suffix '-20191129' glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' glob finding logs to compress failed glob finding old rotated logs failed renaming /var/log/log-file to /var/log/log-file-20191129 creating new /var/log/log-file mode = 0644 uid = 0 gid = 0 running postrotate script
Logrotate定时任务
logrotate需要的cron任务应该在安装时就自动创建了,我把cron文件的内容贴出来,以供大家参考。
[root@master log]# cat /etc/cron.daily/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
logrotate生产应用
防止访问日志文件过大
[root@master ~]# cat /etc/logrotate.d/nginx /var/log/nginx/*.log { su root root daily rotate 5 missingok notifempty dateext olddir /root create 644 root root postrotate if [ -f /var/run/nginx/nginx.pid ]; then kill -USR1 `cat /var/run/nginx/nginx.pid` fi endscript }
logrotate工具对于防止因庞大的日志文件而耗尽存储空间是十分有用的。配置完毕后,进程是全自动的,可以长时间在不需要人为干预下运行。本教程重点关注几个使用logrotate的几个基本样例,你也可以定制它以满足你的需求。
split命令可用选项
短选项 | 长选项 | 选项描述 |
-b | –bytes=SIZE | SIZE 值为每一输出档案的大小,单位为 byte。 |
-C | –line-bytes=SIZE | 每一输出档中,单行的最大 byte 数。 |
-d | –numeric-suffixes | 使用数字作为后缀。 |
-l | –lines=NUMBER | NUMBER 值为每一输出档的列数大小。 |
实例1、以每个文件1000行分割
split命令分割文件成每个文件1000行,并且文件名依次为 [前缀]aa, [前缀]ab, [前缀]ac等,默认的前缀是X,每个文件的行数为1000行,命令如下:
[root@master nginx]# split access.log
[root@master nginx]# wc -l *
2653154 access.log
2653154 access.log.bak
0 error.log
477 error.log.bak
1000 xaa
1000 xab
1000 xac
实例2、以每个文件20MB分割
分割文件为多个20MB的文件,附带-b选项命令如下:
[root@master nginx]# split -b 20M access.log [root@master nginx]# ls -lh 总用量 542M -rwxr-xr-x 1 root root 271M 11月 29 16:24 access.log -rw-r--r-- 1 root root 20M 11月 29 16:24 xaa -rw-r--r-- 1 root root 20M 11月 29 16:24 xab -rw-r--r-- 1 root root 20M 11月 29 16:24 xac -rw-r--r-- 1 root root 20M 11月 29 16:24 xad -rw-r--r-- 1 root root 20M 11月 29 16:24 xae -rw-r--r-- 1 root root 20M 11月 29 16:24 xaf -rw-r--r-- 1 root root 20M 11月 29 16:24 xag -rw-r--r-- 1 root root 20M 11月 29 16:24 xah -rw-r--r-- 1 root root 20M 11月 29 16:24 xai -rw-r--r-- 1 root root 20M 11月 29 16:24 xaj -rw-r--r-- 1 root root 20M 11月 29 16:25 xak -rw-r--r-- 1 root root 20M 11月 29 16:25 xal -rw-r--r-- 1 root root 20M 11月 29 16:25 xam -rw-r--r-- 1 root root 11M 11月 29 16:25 xan
实例3、以每个文件50MB指定前缀分割
使用–bytes选项把文件分割成多个50MB的文件,–bytes类似-b选项,在第二个参数指定前缀。
[root@master nginx]# split --bytes=50M access.log nginx_ [root@master nginx]# ls -lh 总用量 542M -rwxr-xr-x 1 root root 271M 11月 29 16:24 access.log -rw-r--r-- 1 root root 50M 11月 29 16:27 nginx_aa -rw-r--r-- 1 root root 50M 11月 29 16:27 nginx_ab -rw-r--r-- 1 root root 50M 11月 29 16:27 nginx_ac -rw-r--r-- 1 root root 50M 11月 29 16:27 nginx_ad -rw-r--r-- 1 root root 50M 11月 29 16:27 nginx_ae -rw-r--r-- 1 root root 21M 11月 29 16:27 nginx_af
实例4、基于行数分割文件
使用-l选项指定行数来把文件分割成多个行数相同的文件。
[root@master nginx]# wc -l access.log 2653154 access.log [root@master nginx]# split -l 1500 access.log nginx_ [root@master nginx]# wc -l * 2653154 access.log 1500 nginx_aa 1500 nginx_ab 1500 nginx_ac 1500 nginx_ad 1500 nginx_ae 1500 nginx_af
实例5、以数字后缀命名分割文件
使用-d选项可以指定后缀为数字,如00,01,02..,而不是aa,ab,ac。
[root@master nginx]# split -d -b 50M access.log [root@master nginx]# ls -lh 总用量 542M -rwxr-xr-x 1 root root 271M 11月 29 16:24 access.log -rw-r--r-- 1 root root 50M 11月 29 16:31 x00 -rw-r--r-- 1 root root 50M 11月 29 16:31 x01 -rw-r--r-- 1 root root 50M 11月 29 16:31 x02 -rw-r--r-- 1 root root 50M 11月 29 16:31 x03 -rw-r--r-- 1 root root 50M 11月 29 16:31 x04 -rw-r--r-- 1 root root 21M 11月 29 16:31 x05
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?