背景

在生产过程中,由于磁盘空间、保留周期等因素,会对系统、应用等日志提出要求,要求系统日志定期进行轮转、压缩和删除,从而减少开销,而系统自带的logrotate  则是一个简单又实用的小工具,下面着重介绍一下,满足日常需求。

语法

Usage: logrotate [OPTION...] <configfile>

常用参数 :

-f 非设定周期内强制运行

-d 调试,对日志模拟进行操作

-v 可视化执行过程结果

其它参数不常用,详情见下:

[root@test01 ~]# logrotate --help
Usage: logrotate [OPTION...] <configfile>
-d, --debug Don't do anything, just test (implies -v)
-f, --force Force file rotation
-m, --mail=command Command to send mail (instead of `/bin/mail')
-s, --state=statefile Path of state file
-v, --verbose Display messages during rotation
-l, --log=STRING Log file
--version Display version information Help options:
-?, --help Show this help message
--usage Display brief usage message
[root@test01 ~]#

配置文件

logrotate  配置主要有两个

  • 常规主配置文件地址/etc/logrotate.conf 不建议对其修改
  • 自定义配置文件 /etc/logrotate.d 这里存放用户自定义文件,通常建议将个人文件放到这里
[root@test01 ~]# cd /etc/logrotate.d/
[root@test01 logrotate.d]# ll
total 16
-rw-r--r--. 1 root root 91 Aug 6 2019 bootlog
-rw-r--r--. 1 root root 224 Aug 6 2019 syslog
-rw-r--r--. 1 root root 100 Oct 31 2018 wpa_supplicant
-rw-r--r--. 1 root root 103 Aug 8 2019 yum
[root@test01 logrotate.d]#

为保证 /etc/logrotate.d 定义的文件能够执行生效,需要确保 /etc/logrotate.conf 这句话没有注释,如有注释,需手动取消:

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

配置文件demo及参数

以下是我个人一个配置文件,可以直接拿来结合自己需求进行修改:

/var/log/linuxserver/linux.log {
rotate 7
size 200M
daily
compress
delaycompress
missingok
notifempty
noolddir
copytruncate
dateext
dateformat .%s
create 660 root root
}

文件格式主要是以日志路径和日志切割参数组成:

第一行:指明 需要进行切割的日志路径,可以结合正则表达式,例如 /path/*.log ,指定路径下所有的日志

第二行:以{ } 包含logrotate 所需要的参数,demo包含了基本常用到的几个参数,此外还有如下部分参数,参数含义如下:

# 日志压缩参数
compress 通过gzip 压缩转储以后的日志
delaycompress 和compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩
nocompress 不做gzip压缩处理
# 对截断后的日志处理参数
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。
nocopytruncate 备份日志文件不过不截断
create mode owner group 轮转时指定创建新文件的属性,如create 0777 nobody nobody
nocreate 不建立新的日志文件
dateext 使用当期日期作为命名格式
dateformat .%s 配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数
# 基本参数
missingok 如果日志丢失,不报错继续滚动下一个日志
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使日志文件为空文件也做轮转,这个是logrotate的缺省选项。
notifempty 当日志文件为空时,不进行轮转
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
# 旧日志存放参数
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
# logrotate 执行前后钩子参数
sharedscripts 运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本
prerotate 在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;必须独立成行
postrotate 在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 某个服务!必须独立成行
# 执行周期参数
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
# logrotate 触发条件参数
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
size(或minsize) log-size 当日志文件到达指定的大小M、G时才转储

执行触发时间

logrotate 默认走的是crond 服务 ,执行语句脚本存放在/etc/cron.daily/ 目录下,主要脚本:/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf

默认触发时间配置文件存在 /etc/anacrontab ,默认执行时间 每日3.45-22.45 时间段随机执行一次。缺点是具体执行时间不容易掌握,通常不建议用户对该文件进行修改,如果需要定期执行,建议用户可以在vim /etc/crontabcrontab  -e 按照crond 格式设定执行时间,以便更好的分配脚本执行时间,避免机器资源集中使用,减少机器负载。

logrotate command in Linux的更多相关文章

  1. Sending Email from mailx Command in Linux Using Gmail’s SMTP

    The mailx or mail command in Linux is still providing service for guys like me, especially when we n ...

  2. Using Android Phone to recover SD card formatted with DD command under linux

    Using Android Phone to recover SD card formatted with DD command under linux 1. Formatted a sd card ...

  3. screen command of linux

    [screen command of linux] 常用键:   补充: Ctrl-a S  # split terminal horizon Ctrl-a TAB   # switch to ano ...

  4. 下载tree命令的源代码 - The Tree Command for Linux Homepage

    The Tree Command for Linux Homepage http://mama.indstate.edu/users/ice/tree/ [root@test ~]# ll -as m ...

  5. Changing the Color of Linux ls Command 改变Linux的ls命令显示的颜色

    Linux command ls basically use the file /etc/DIR_COLORS or /etc/DIR_COLORS.xterm to define the color ...

  6. The dd command of linux

    The dd command stands for "data duplicator" and used for copying and converting data. It i ...

  7. To Use FTP Command in Linux

    Yesterday I was asked to upload a file in Linux to the corresponding server. I said "oops" ...

  8. Switch User Command in Linux

    Switch user command (su) has the following forms: su Switch to , without loading environment variabl ...

  9. Learn sed using these command on Linux(流线式编辑器——sed)

    是对文件中的每一行进行处理,不会对源文件进行修改 sed --version sed '11d' sed_file sed -n '/[Bb]erry/p' sed_file (由于设置了n,所以只打 ...

随机推荐

  1. Flutter和iOS混编详解

    前言 下面的内容是最近在使用Flutter和我们自己项目进行混编时候的一些总结以及自己踩的一些坑,处理完了就顺便把整个过程以及一些我们可能需要注意的点全都梳理出来,希望对有需要的小伙伴有点帮助,也方便 ...

  2. 新作!分布式系统韧性架构压舱石OpenChaos

    摘要:本文首先以现今分布式系统的复杂性和稳定性的需求引出混沌工程概念,并阐述了OpenChaos在传统混沌工程上的优化与创新. 背景 随着Serverless,微服务(含服务网格)与越来越多的容器化架 ...

  3. 好客租房48-组件的props(基本使用)

    组件是封闭的 要接受外部数据应该通过props来实现 props的作用:接受传递给组件的数据 传递数据:给组件标签添加属性 接收数据:函数组件通过参数props接收数据 类组件通过this.props ...

  4. 【freertos】008-内存管理

    前言 本章主要讲解内部存储空间(RAM)的管理. 详细分析heap5方案. 参考: 李柱明博客 https://freertos.blog.csdn.net/article/details/51606 ...

  5. OpenWrt 20.02.2 小米路由器3G配置CP1025网络打印

    家里的施乐 CP116w 工作快五年了终于罢工了. 黑粉报错, 自己也不会拆, 只能搁置了. 后来换了个 HP CP1025. 这个打印机也不错, 墨盒便宜没什么废粉, 就是启动慢一点, 而且 -- ...

  6. 大白话讲Java的锁

    偏向锁 对一个对象的锁偏向于某个线程,在markword中记录线程id 下次相同的线程来,直接就可以获取锁 轻量级锁 对象的Markword记录锁地址 跟线程栈里面的锁记录Lock Record的锁地 ...

  7. github新项目npm错误

    当我们从GitHub或者别人那里拿到项目的时候,一般都是要先npm install 进行安装依赖.但是难免会遇到报错. 出现问题1: 解决方案:清除缓存npm cache clear --force之 ...

  8. 超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群

    前言 干货又来了,全程无废话,可先看目录了解. MySQL搭建集群最常见的是binlog方式,但还有一种方式是强一致性的,能保证集群节点的数据一定能够同步成功,这种方式就是pxc,本篇就使用图文方式一 ...

  9. AspNetCore&云效Flow持续集成

    如今有了越来越多的持续集成工具,给的个人开发者的福利也是很足了,如无必要,自建工具有时只是作为练手了. 众多持续集成工具 现在可用的持续集成工具繁多,各大云服务商都推出了持续集成,甚至是一定条件内都是 ...

  10. Spring框架系列(6) - Spring IOC实现原理详解之IOC体系结构设计

    在对IoC有了初步的认知后,我们开始对IOC的实现原理进行深入理解.本文将帮助你站在设计者的角度去看IOC最顶层的结构设计.@pdai Spring框架系列(6) - Spring IOC实现原理详解 ...