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

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

off999 2025-02-16 22:25 31 浏览 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

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

相关推荐

无网络单机游戏(好玩的无网络单机游戏)

一款能够让我们自由畅快的进行游戏的合集软件。在这个合集之中有着各种各样的单机小游戏。这些小游戏不需要联网就可以玩了,没有防沉迷系统,我们想玩多久就玩多久,合集之中小游戏的类型有很多种,有赛车类、射击类...

笔记本电脑推荐理由(推荐笔记本子)

配置落后散热出现问题是笔记本报废的主要原因因为旧电脑问题很多。一是现在新电脑价格不算贵,没必要用旧的,电脑可不兴新不如旧的说法;二是二手电脑是否大修过,是否有什么瑕疵,是否运行速度有问题,是否被人监控...

tplink设置向导(tp link路由器的设置向导)

tplink路由器首次设置时才会自动弹出向导,如果希望自动弹出,可先还原出厂设置,然后再登录即可自动弹出,还原出厂设置步骤如下:  1、路由器开启电源;  2、按下路由器表面的reset复位按钮,个别...

破解苹果激活锁的万能id(破解iphone id激活锁软件)

提前打开我的AppleID界面,点击管理您的账户。点击忘记AppleID,填写与AppleID相关的各项基本信息。通过电子邮件内的链接或回答安全问题,或者进行人工破解。1、登陆苹果官网,点击下面...

手机上不了wifi是什么原因(手机上不了网连不了wifi)

1.手机wifi已连接不可上网问题出现后首先检测一下是否为手机本身突发性的问题,可以进行关机重启一下再连接看是否还会出现这样的情况。或者使用其他手机或设备连接WiFi试试能否上网。2.如果不是手机的问...

桌面图标大小设置(怎么修改桌面图标大小设置)
  • 桌面图标大小设置(怎么修改桌面图标大小设置)
  • 桌面图标大小设置(怎么修改桌面图标大小设置)
  • 桌面图标大小设置(怎么修改桌面图标大小设置)
  • 桌面图标大小设置(怎么修改桌面图标大小设置)
win10有产品id没有密钥(win10 我没有产品密钥)

WIN10,在左下角输入WINDOWSPOWERSHELL,然后点击系统查询结果第一项,在弹出的窗口中输入:(Get-WmiObject-query‘select*fromSoftware...

win10截屏后找不到了(win10截屏后找不到了怎么办)

Win10系统截屏后没有更新通知并不是一个常见的问题。可能是由于系统设置或者安装的软件造成的。如果系统设置为静音或者禁用通知,那么截屏后不会有通知。另外,一些截屏软件可能会阻止系统通知,需要手动设置...

微软官网win10家庭版下载(微软官网win10家庭版下载速度慢)
  • 微软官网win10家庭版下载(微软官网win10家庭版下载速度慢)
  • 微软官网win10家庭版下载(微软官网win10家庭版下载速度慢)
  • 微软官网win10家庭版下载(微软官网win10家庭版下载速度慢)
  • 微软官网win10家庭版下载(微软官网win10家庭版下载速度慢)
运行定时关机命令(运行定时关机命令怎么设置)

1、打开电脑,按住【Win+R】组合键,弹出运行命令,在编辑框内输入如下命令:shutdown-s-t3600;电脑定时关机运行2、shutdown-s-t3600命令的含义如下:shut...

win7打印机共享设置(详细图文步骤)
  • win7打印机共享设置(详细图文步骤)
  • win7打印机共享设置(详细图文步骤)
  • win7打印机共享设置(详细图文步骤)
  • win7打印机共享设置(详细图文步骤)
键盘快捷键(键盘快捷键是哪个)

电脑键盘快捷键大全:Ctrl+1,2,3... 功能:切换到从左边数起第1,2,3...个标签Ctrl+A 功能:全部选中当前页面内容Ctrl+C 功能:复制当前选中内容Ctrl+D 功能:打开“添加...

宏基acer官网商城(宏基官方商城)

宏碁本本底部有个序列号标签,不是粉色的那个,粉色的是系统的序列号,白色的是本本的出厂序列号,上宏碁官网查查你的序列号就知道真假了。在盖子的后面就可以查询到序列号,或者是去网上查询宏碁笔记本在官网下...

网盘在哪里打开(华为网盘在哪里打开)

给你的是一个网盘地址吧,直接把这个地址放到浏览器地址栏就好了,如果有提取码,他会提示你输入提取码的。一般都是在网上找到百度云链接,然后把文件保存在自己的百度网盘文件夹里面的。打开百度网盘app,...

win10系统安全模式怎么进入(win10安全模式如何进入)
  • win10系统安全模式怎么进入(win10安全模式如何进入)
  • win10系统安全模式怎么进入(win10安全模式如何进入)
  • win10系统安全模式怎么进入(win10安全模式如何进入)
  • win10系统安全模式怎么进入(win10安全模式如何进入)

取消回复欢迎 发表评论: