百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Linux 使用 logrotate 日志归档_linux日志管理

off999 2025-02-16 22:25 22 浏览 0 评论

系统时时刻刻都在产生日志,如果不及时清理,很快就会灌满硬盘,但如果要手工清理,又很麻烦。

这种情况下,logrotate 这个程序很好的完成这个任务。

logrotate 用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”。

我们可以根据日志文件的大小,也可以根据其天数来转储,这个过程一般通过一个叫做crond的守护进程来执行,logrotate 还可以用于压缩日志文件,以及发送日志到指定的E-mail 。

logrotate 的配置文件是 /etc/logrotate.conf。

查看当前版本

在系统中使用命令来查看版本:

logrotate --version

一些使用的方法,请参考下面的内容:

查看使用手册

man logrotate

nginx 日志归档

在默认情况下,操作系统已经为我们配置了 nginx 的默认日志归档。

归档的配置文件为: /etc/logrotate.d/nginx

可以直接编辑这个文件。

/var/log/nginx/*.log
/var/log/nginx/src.isharkfly.com/*.log
{
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 0640 www-data adm
        sharedscripts
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi \
        endscript
        postrotate
                invoke-rc.d nginx rotate >/dev/null 2>&1
        endscript
}

对我们来说,我们虚拟主机的日志和 nginx 的日志在相同的目录下,只是在下面添加了一个文件夹。

所以,我们就在上面添加文件夹就行。

归档测试

配置文件完成修改后,可以对配置进行测试:

运行命令:

logrotate /etc/logrotate.d/nginx --debug

服务器上输出的内容为:

root@ns564012:/etc/logrotate.d# logrotate /etc/logrotate.d/nginx --debug
warning: logrotate in debug mode does nothing except printing debug messages!  Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.d/nginx
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state

Handling 1 logs

rotating pattern: /var/log/nginx/*.log
/var/log/nginx/src.isharkfly.com/*.log
 after 1 days (14 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  Now: 2025-02-06 02:38
  Last rotated at 2025-02-06 00:00
  log does not need rotating (log has been rotated at 2025-02-06 00:00, which is less than a day ago)
considering log /var/log/nginx/error.log
  Now: 2025-02-06 02:38
  Last rotated at 2025-02-06 00:00
  log does not need rotating (log has been rotated at 2025-02-06 00:00, which is less than a day ago)
considering log /var/log/nginx/src.isharkfly.com/access.log
Creating new state
  Now: 2025-02-06 02:38
  Last rotated at 2025-02-06 02:00
  log does not need rotating (log has already been rotated)
considering log /var/log/nginx/src.isharkfly.com/error.log
Creating new state
  Now: 2025-02-06 02:38
  Last rotated at 2025-02-06 02:00
  log does not need rotating (log has already been rotated)
not running prerotate script, since no logs will be rotated
not running postrotate script, s

对我们来说,只需要日志进行归档就行,因为我们不希望日志内容占用过多的磁盘空间。

相关推荐

一招实现让Python Numpy快到飞起!

在本文中,笔者将介绍Numexpr这个强大的Python库,它可以显著提升NumPy数组的计算性能。Numexpr是一个专门用于提高NumPy数组计算性能的工具。它通过避免为中间结果分配内存,提高了缓...

通过anaconda安装python及人工智能框架pytorch安装

专栏推荐python2和python3的切换Linux安装好了之后,一般有python2和python3,linux默认的是python2,但是现在python可能马上就要被淘汰了,所以我们平时都是使...

VBA宏总崩溃?Python脚本老闪退?这招让你的代码从此“抗摔”!

导语:上周有个读者急吼吼地私信我:“老师,我写了个VBA宏整理半年数据,结果跑到第800行的时候弹出个Error1004,前功尽弃!有没有办法让宏‘抗摔’?”还有位Python新手更崩溃:“明明写了...

六种程序设计语言的循环及循环嵌套之比较(周兴富)

我在《72岁老翁学python》(六)一文里展示了仿写的第一个python程序zxf-python-cx01.py,以及试写的一个程序zxf-python-cx02.py,虽然错误多多,但是万里长征第...

简单学Python——关键字6——break和continue

Python退出循环,有break语句和continue语句两种实现方式。break语句和continue语句的区别:break语句作用是终止循环。continue语句作用是跳出本轮循环,继续下一次循...

Python循环的简单写法_python 循环写法

在之前的短文里归纳总结了Shell的循环写法,里面也提到了Python的循环,这里也对Python的循环做个简单的说明。Python的循环基本上2种:while循环和for-in循环,和Shell还是...

Python循环语句:从基础到进阶_python里面的循环语句

在Python编程语言中,循环语句是必不可少的控制流工具。它们允许重复执行一段代码,直到满足特定条件。本文将介绍Python中的两种主要循环类型:for循环和while循环,并分别结合实例来说明其用法...

Python教程(十):for循环和range()函数

今天我们将深入探讨非常有用的**for循环和内置的range()函数**—这两个工具让您能够重复执行操作并高效地遍历序列。让我们探索如何使用它们以及它们在哪些地方表现出色。今天您将学习什么什么...

《循环(for/while)》_循环for的用法

循环(for/while)循环是编程中处理重复任务的核心工具,Python提供了两种主流循环结构:for循环(遍历可迭代对象)和while循环(根据条件重复执行)。本节将系统讲解两者的语法、使用场景及...

Python循环结构精解:掌握while循环与累加算法

循环结构是程序三大基本结构之一,其本质是反复执行相同操作直到满足特定条件。现实世界中的循环无处不在:旋转的车轮人体的血液循环每日重复的学习过程Python循环分类while循环:基于条件判断的循环fo...

Python教程(十一):循环控制 – break、continue和pass

昨天,我们探索了for循环和range()函数。今天,我们将更进一步,学习如何使用三个强大的工具来控制循环内部的流程:break、continue和pass。这些命令让您能够:停止循环(break)...

AI最火语言python之循环结构_python语言中的循环结构

循环结构是指在某种情况下反复执行某段代码,其中被反复执行的代码段称之为循环体,循环结构主要负责完成重复性的工作。有for循环语句和while循环语句实现循环结构。1.for循环语句for语句主要用于实...

Python入门学习教程:第 5 章 循环语句

5.1什么是循环语句?在编程中,我们经常需要重复执行某段代码。例如,计算1到100的总和、打印列表中的所有元素等。如果手动编写每一次的执行代码,不仅繁琐,还容易出错。循环语句就是用来解决这种...

Python之Web开发框架学习-Django基础及安装

Django是一个高级PythonWeb框架,鼓励快速开发和简洁实用的设计。Django使使用更少的代码快速构建更好的Web应用程序变得更加容易。注-Django是DjangoSoftware...

Python基础入门- Python编程环境搭建-Python编译器安装

Python3下载访问https://www.python.org/进行下载Python安装双击下载的“.exe”文件即可安装手动配置python环境变量如果是在安装过程中,勾选了配置环境变量...

取消回复欢迎 发表评论: