如何用logrotate管理每日增长的日志

news2024/10/7 4:30:33

这篇文章主要介绍了如何用logrotate管理每日增长的日志问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教!

logrotate简介

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

这是logrotate文档中对其自身的描述,其实logrotate功能很简单,正如其名,按一定周期自动循环分割和保存日志防止无限增长,必要的时候进行压缩、删除,还可以进行邮件通知等功能。

很多服务器上总会运行着一些会不停生成成吨日志的程序,最简单的比如nginx、httpd之类的web服务器的访问日志,为了保持这些日志的可检索以及防止无限增长把有限的硬盘都吃干净了,就会有五花八门的cron脚本来处理这些日志,事实上这些事情都可以由logrotate代劳。

安装logrotate

事实上大多数Linux发行版本都默认安装了logrotate,可以通过以下命令来判断系统是否已经预装了logrotate:

rpm包管理器:

rpm -qa|grep logrotate

dpkg包管理器:

dpkg --list|grep logrotate

更简单粗暴的方法:

which logrotate

如果能看到有字符串返回的话,你的服务器就应该预装了logrotate,并且它已经每天在默默地工作了,只不过你并没有给他分配活儿。

如果没有安装的话,用正常的包管理工具都能安装它:

yum install logrotateapt-get install logrotate

ogrotate基本工作原理

        作为一个定时处理的管理工具,logrotate本身是个命令行的工具,然而它需要定时处理你的日志,所以显然logrotate还是得基于cron来工作的,否则就没有人定时来唤醒它让它干活了,正常情况下logrotate已经作为cron.daily的一个任务每天被定时在执行了,这个定时脚本位于/etc/cron.daily/logrotate

#!/bin/sh
# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
    [ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

上面这段是Ubuntu中默认的logrotate脚本,各个不同版本上的logrotate脚本会有细微的不同,但是核心都是使用/etc/logrotate.conf配置脚本来启动logrotate。

然而这段脚本只是位于这个目录里,为什么会被每天调用呢,而且每天什么点调用呢?

可以查看CRON的默认配置文件/etc/crontab(新版CentOS中其默认配置文件位于/etc/anacrontab)的内容:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

事实上CRON默认会存在这4条指令,分别是每小时、每天、每周、每个月会执行对应目录下的脚本,而不需要再通过日常常用的crontab去配置,可以看到cron.daily目录下的脚本在每天的6点25分会被执行,这也就意味着在这台机器上每天6点25分会启动logrotate进行日志的处理,各个系统的默认配置也是各不相同的。

最后来看一下默认的配置文件/etc/logrotate.conf:

# see "man logrotate" for details
# rotate log files weekly
weekly
# use the syslog group by default, since this is the owning group
# of /var/log/syslog.
su root syslog
# 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
# packages drop log rotation information into this directory
include /etc/logrotate.d
# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}
/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}
# system-specific logs may be configured here

        其实默认配置文件中给出的是logrotate的一些默认配置选项,并给出了一些样例的配置(如wtmp,btmp),这些配置的意义在后文中会予以阐述,而这个配置文件中还包含了include /etc/logrotate.d,这使得我们可以不用将所有的配置扎堆写入这个默认配置文件中,而是可以分散地管理在/etc/logrotate.d目录下,事实上/etc/logrotate.d也已经包含了一些样例的配置文件供参考。

logrotate的配置和使用

        如同刚才所说的,logrotate会每日CRON启动并执行,我们只需要在/etc/logrotate.d目录中添加我们需要的配置,就可以利用logrotate来自动管理我们需要的日志文件。

这里先来看一个目录下已经存在的样例配置dpkg:

/var/log/dpkg.log {
    monthly
    rotate 12
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
}
/var/log/alternatives.log {
    monthly
    rotate 12
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
}

正如其名称dpkg,这个配置是用来处理包管理器dpkg的默认日志的,处理的日志文件是/var/log/dpkg.log与/var/log/alternatives.log这两个文件

其配置内容如下:

  • monthly:按月处理日志
  • rotate 12:保留12份日志
  • compress:日志分割后会进行gzip压缩
  • delaycompress:日志压缩会被延后到下次分割时进行
  • missingok:目标日志文件不存在程序也不会报错退出
  • notifempty:目标日志文件为空时不进行分割操作
  • create 644 root root:以644也就是rw-r–r–权限来建立分割出来的文件,同时该分割文件所属用户为root,用户组为root

在/var/log下列出文件列表,可以看到dpkg的日志的确被分割并压缩了:

仿照这个样例文件,我们可以配置一个类似的处理任务,这里处理一下nginx的log文件,一般web serve的access log文件都是日志重灾区,建立一个/etc/logrotate.d/nginx配置文件,内容如下:

/home/wwwlogs/*.log {
    daily
    rotate 7
    dateext
    compress
    delaycompress    
    missingok
    notifempty
    create 644 root root
    sharedscripts
    postrotate
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
    endscript
}

与之前的样例配置类似,注意到声明日志文件位置时使用了*通配符,这意味着/home/wwwlogs/下的所有.log文件都会被logrotate处理,这里多出了几行配置

说明如下:

  • daily:每天处理日志
  • dateext:备份的日志文件的后缀不再是默认的.1 .2 .3的递增格式,而是.YYYYMMDD的日期格式
  • sharedscripts:配置该选项时,prerotate和postrotate段的脚本会在所有文件被处理结束(被处理前)统一执行,而不是每个文件前后分别执行一次
  • postrotate:处理日志后钩子,postrotate和endscript构成一个shell语句块,在日志文件被处理后这部分语句会被执行,对应的还有prerotate语句块

这样nginx的log文件的处理配置就完成了,但是我们可能还需要确认一下配置的内容到底是否正确呢,这时候可以利用logrotate命令行的debug选项来进行测试

命令如下:

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

开启了debug选项时,logrotate会详细地给出处理日志过程中的处理信息,但是并不会真正地去处理日志文件,所以可以用来测试配置文件处理的是否正确

这行命令的输出如下:

reading config file /etc/logrotate.d/nginx
Handling 1 logs
rotating pattern: /home/wwwlogs/*.log  forced from command line (7 rotations)
empty log files are not rotated, old logs are removed
considering log /home/wwwlogs/access.log
  log needs rotating
considering log /home/wwwlogs/jp01.sanaecon.com.log
  log needs rotating
considering log /home/wwwlogs/nginx_error.log
  log needs rotating
rotating log /home/wwwlogs/access.log, log->rotateCount is 7
dateext suffix '-20151108'
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
rotating log /home/wwwlogs/jp01.sanaecon.com.log, log->rotateCount is 7
dateext suffix '-20151108'
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
rotating log /home/wwwlogs/nginx_error.log, log->rotateCount is 7
dateext suffix '-20151108'
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 /home/wwwlogs/access.log to /home/wwwlogs/access.log-20151108
creating new /home/wwwlogs/access.log mode = 0644 uid = 0 gid = 0
renaming /home/wwwlogs/jp01.sanaecon.com.log to /home/wwwlogs/jp01.sanaecon.com.log-20151108
creating new /home/wwwlogs/jp01.sanaecon.com.log mode = 0644 uid = 0 gid = 0
renaming /home/wwwlogs/nginx_error.log to /home/wwwlogs/nginx_error.log-20151108
creating new /home/wwwlogs/nginx_error.log mode = 0644 uid = 0 gid = 0
running postrotate script
running script with arg /home/wwwlogs/*.log : "
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
"

可以看到logrotate如我们设想的那样在正常处理日志文件,至此一个logrotate配置就完成了。

        logrotate命令行除了可以用来展示配置文件配置是否正确以外,还可以用来手动执行日志分割,使用以下命令行:

logrotate -f /etc/logrotate.d/nginx

        就会脱离CRON手动运行一次日志分割和处理任务,事实上通过这个方式也可以用第三方的其他程序来管理日志分割的频率。本文中给出了一些常见的配置文件中的配置参数的含义,更多其他的配置参数的含义和功能可以参考官方的配置文档:http://linuxconfig.org/logrotate-8-manual-page

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/723307.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据结构--二叉树的存储结构

数据结构–二叉树的存储结构 二叉树的顺序存储 #define MaxSize 100 struct TreeNode {ElemType value;bool isEmpty; }; TreeNode tree[MaxSize];初始化 void init() {for (int i 0; i < MaxSize; i)tree[i].isEmpty true; }几个重要常考的基本操作: i的左孩子: 2 i 2…

骨传导蓝牙耳机哪个好,推荐几款造诣不错的骨传导耳机

欢迎来到骨传导耳机的奇妙世界&#xff01;这种别具一格的耳机&#xff0c;也被亲切地称为“不入耳式”耳机。它采用了一种神奇的技术&#xff0c;通过颅骨、骨迷路、内耳淋巴液和听神经之间的信号传导&#xff0c;保护我们的听力。不仅如此&#xff0c;这款耳机的开放式设计&a…

Spring七大组件

一、Spring七大组件 1.核心容器(Spring core) Spring-core相当于一个创建并管理bean的容器&#xff1a; 创建&#xff1a;底层使用反射技术创建bean的实例&#xff1b;管理&#xff1a;容器中的每个bean&#xff0c;spring容器默认按照单例模式管理&#xff1b;设计&#xff1…

0501逻辑存储结构和架构-InnoDB引擎-MySQL-数据库

文章目录 1 逻辑结构1.1 表空间1.2 段1.3 区1.4 页1.5 行 2 架构2.1 内存架构2.1.1 Buffer Pool2.1.2 change bufer2.1.3 自适应哈希2.1.4 log buffer 2.2 磁盘架构2.2.1 System Tablespace2.2.2 File-Per-Table Tablespace2.2.3 General Tablespaces2.2.4 Undo Tablespaces2.2…

NC30 缺失的第一个正整数

import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定&#xff0c;请勿修改&#xff0c;直接返回方法规定的值即可*** param nums int整型一维数组* return int整型*/public int minNumberDisappeared (int[] nums) {// write code hereHashM…

CCF-CSP真题《202303-3 LDAP》思路+python,c++满分题解

想查看其他题的真题及题解的同学可以前往查看&#xff1a;CCF-CSP真题附题解大全 试题编号&#xff1a;202303-3试题名称&#xff1a;LDAP时间限制&#xff1a;12.0s内存限制&#xff1a;1.0GB问题描述&#xff1a; 题目背景 西西艾弗岛运营公司是一家负责维护和运营岛上基础设…

Android Studio实现内容丰富的安卓电影购票系统

如需源码可以添加q-------3290510686&#xff0c;也有演示视频演示具体功能&#xff0c;源码不免费&#xff0c;尊重创作&#xff0c;尊重劳动。 项目编号043 1.开发环境 android stuido jdk1.8 eclipse mysql tomcat 2.功能介绍 安卓端&#xff1a; 1.注册登录 2.查看电影列表…

datagrip 无法断句

使用datagrip 的时候出现了几个sql 执行会无法断开语句的情况可以在这里设置: When caret outside statement execute:

PADS Layout软件中焊盘各层的释义是什么呢

对于PADS软件而言&#xff0c;焊盘的组成由下面几种组成。 焊盘&#xff1a;包括规则焊盘以及通孔焊盘。 热焊盘&#xff1a;热风盘&#xff0c;也叫花焊盘&#xff0c;在负片中有效&#xff0c;设计用于在负片中焊盘与敷铜的接连方式&#xff0c;防止焊接时散热太快&#xf…

Windows远程连接linux中mysql数据库

我没有mysql并且没有把mysql配置到环境变量中&#xff0c;所以现在我要下载mysql 一.下载mysql Mysql官网下载地址&#xff1a;https://downloads.mysql.com/archives/installer 二.安装mysql 1. 选择设置类型 双击运行mysql-installer-community-8.0.26.msi&#xff0c;这…

网络安全 | 商用密码产品介绍

关注wx&#xff1a; CodingTechWork 密码产品概述 密码 使用特定变换对数据等信息进行加密保护、安全认证的技术、产品和服务。 密码技术 实现密码的加密保护和安全认证等功能的技术&#xff0c;主要包含密码算法、密钥管理和密码协议等。 密码划分 核心密码普通密码商用…

ai绘画图片网站哪个好?这几个好用的ai绘画图片网站分享给你

近期我的朋友公司给它安排了一项艰巨的任务&#xff0c;公司给了他一堆图片&#xff0c;然后要求他根据这些图片生成一些风格类似却又独具特色的优质图片&#xff0c;这可难倒他了&#xff0c;他在机缘巧合的情况下得知了有ai绘画图片生成图片的技术&#xff0c;但它不知道怎么…

Rust 第三天---内存管理与所有权

前面介绍了环境配置以及基础语法,掌握之后已经可以开始用Rust编写一些简单的程序了,今天就要来介绍一下Rust核心的功能—内存管理与所有权 1. 关于内存管理 无论什么高级语言必须考虑到的一点就是编写程序时对于内存的管理问题,更简单一点解释,利用编程语言能快速高效的分配内…

C语言究竟是一门怎样的语言?

对于大部分程序员&#xff0c;C语言是学习编程的第一门语言&#xff0c;很少有不了解C的程序员。 C语言除了能让你了解编程的相关概念&#xff0c;带你走进编程的大门&#xff0c;还能让你明白程序的运行原理&#xff0c;比如&#xff0c;计算机的各个部件是如何交互的&#xf…

40、使用elementUI分别实现前端,后端表格分页

说明&#xff1a;前端单独做的表格分页—用于解决数据过多页面渲染压力&#xff0c;如果是服务器响应数据过慢&#xff0c;使用第二种分页方法–后端分页。以下为分页效果 一、前端分页 1、创建表格 <el-table:key"new Date().getTime()":data"tableData.s…

Linux samba服务器配置教程

此教程适用于Centos 和 Ubuntu&#xff0c;其它Linux系统一般大概率配置命令相同&#xff01; 一、关闭防火墙 1. Centos 查看防火墙状态&#xff1a;systemctl status firewalld.service 关闭防火墙&#xff1a;systemctl stop firewalld.service 永久关闭防火墙&#xff…

Node中npm基本使用

1.创建文件夹 说明&#xff1a;创建一个空文件夹&#xff0c;然后以此目录为工作目录&#xff0c;执行npm init,交互式创建package.json文件(包的配置文件) npm init 注意&#xff1a;package下的name属性不能使用中文&#xff0c;大写&#xff0c;默认值为文件名的名称&…

eNSP-ACL分类

ACL分类 文章目录 ACL分类一、题目要求二、题目分析三、拓扑结构四、基本配置五、测试验证 一、题目要求 1 、 client1能够 ping通server&#xff0c;但是不能telnet 2 、 client2能够 telnet, 但是不能 ping 通server 3 、 使用一张 ACL列表 二、题目分析 使用高级ACL在AR3的…

商用车自动驾驶线控底盘测试报告

一&#xff0e;概述 商用车线控底盘主要用于接收控制指令&#xff0c;完成相应的驱动、档位、制动、转向、声光等动作&#xff0c;从而实现自动驾驶功能。 底盘线控系统测试目的是对驾驶模式&#xff08;Drive Mode&#xff09;、油门&#xff08;Throttle&#xff09;、档位&a…

window10环境下安装pycocotools报错及解决办法

相信大家入门安装相关深度学习项目时可能会遇到相关pycocotools&#xff0c;或者ninja的相关报错&#xff0c;下面图是网上拿的&#xff0c;真实报错我忘了截图&#xff0c;但是报错信息如果提到安装Microsoft Visual Tolls&#xff0c;大概率这个可行。 在网上找了很多推荐的…