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

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

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

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

相关推荐

surface售后客服(surface售后电话是多少)

是因为以前有人把保修条例玩坏了。。微软Surface系的产品,在之前是非常良心的,中国区和其他地区一样,享有全球联保(买的什么行都保修)、在保配件免费更换(比如SurfacePen一年内用坏了,联系...

文档如何加密设置密码(如何让文档加密码)

你好,要在手机的电子邮件应用中设置电子邮件帐户,一般需要你先在相关邮箱网页设置中确保已经开启POP/IMAP/SMTP功能。像QQ邮箱中,就是登录邮箱网页版之后,点击设置,在帐户选项卡中开启POP3/...

windows7enterprise激活码(win7sp1激活码)

激活方法:激活windows7激活方法首先下载暴风激活工具,解压出来,直接双击打开,或者右键管理员打开;接着激活工具自动识别到windows7版本,点击【一键永久激活windows和Office】,...

控制面板在哪里打开win7(控制面板在哪里打开联想)

方法1、开始打开法点击“开始”按钮。选择找到“控制面板”。点击鼠标左键打开。方法2、桌面打开法双击桌面上的“计算机”,打开后选择”打开控制面板“选项。方法3、系统属性打开法打开“计算机”,可以是开始菜...

program是什么意思(program是什么意思英语)

ProgramFiles是程序文件,就是说Windows操作系统,当然也包括其它的操作系统各种软件默认安装到的目录,位于C盘分区("C:ProgramFiles","%...

电脑插耳机却是扬声器(电脑插耳机却是扬声器声音)

这种情况可能是因为电脑的音频设置有误导致的。可以尝试以下方法解决:1.检查耳机插头是否插紧。有时插头没有插紧会导致电脑仍然识别为扬声器。2.检查电脑的音频设置。打开“控制面板”,选择“硬件和声音”...

win7进入bios怎么修复系统(win7进入bios设置界面)

你好,如果您的电脑已经进入了BIOS,那么您可以按照以下步骤来恢复系统WIN7:1.打开电脑并进入BIOS界面,找到“Boot”选项卡。2.在“Boot”选项卡中,选择您的CD/DVD驱动器作为启...

被写保护的u盘还能用吗(被写保护的u盘怎么办)

一般U盘上有个写保护开关,你把那个开关拨到相反的位置就OK了!然后就可以格式化了!如果没有写保护开关,有个U盘病毒专杀软件:USBCleaner,它自带了一个附件,有U盘的开启和关闭写保护功能,自...

wps与word文档一样吗(wps和word是一样的吗)
wps与word文档一样吗(wps和word是一样的吗)

WPS和Word的区别:1、功能分析WORD功能强大适用范围大,WPS设置不了的功能WORD可以实现;2、兼容性WORD的兼容性相比WPS更好一些,但是只是平常办公WPS已经够用了。如果要打印的文件要到其它机上打开或者打印、在网上发送邮件,...

2025-11-26 13:03 off999

win7修改兼容性依旧不能运行
win7修改兼容性依旧不能运行

1、在电脑的光驱上面插入win7系统的光盘,然后系统加载光驱后自动弹出“运行setup.exe”文件;(如果不自动弹出,单击“计算机”--“DVD驱动器”中的Windows7然后双击setup.exe运行。)2、在“安装Wind...

2025-11-26 12:51 off999

ios微信多开(免费版)2025(iphone微信多开版免费)

现在很多的国产手机都自带了微信分身功能:比如华为、小米、oppo、锤子等,而且稳定好用不会封号,但如果再想开一个分身应用的话,安卓机可以下载一个双开助手,点开以后复制出一个微信分身即可,也很稳定,不封...

主题下载网站(3ds主题下载网站)

1.进入苹果手机系统后,长按手机的空白区域。2.长按进入编辑界面后,点击左上方的加号图标。3.点击后,选择需要添加的小主题。4.点击后,滑动选择需要使用的样式,选择下方的添加选项。5.之后,小组件就会...

联想thinkpad e480(联想ThinkPad E480 i3 7代是那一年的)

可以。1.电脑重启或开机,在出现联想Logo的时候,不停地按键盘F1或FN+F1,进入BIOS,接着切换到Security选项卡,然后选择到SecurityChip选项,2.可以看到Securi...

ghost win10与原版有什么区别

Windows10原版操作系统是微软公司出来的,它没有进行精简,稳定性很高。而ghost版本系统是由个人将一些常用软件安装进去和删除一些系统不常用的组件的一个压缩系统,它安装速度快,但往往会植入一些病...

macbookair恢复出厂设置(macbookair恢复出厂设置后无法开机)
macbookair恢复出厂设置(macbookair恢复出厂设置后无法开机)

1.进入主屏幕,然后点击苹果图标。2.在下拉菜单中点击系统偏好设置。3.再次点击左上角的系统偏好设置,选择抹掉所有内容和设置即可恢复出厂设置。1.打开电脑,点左上角的苹果标志2.点系统偏好设置3.点抹掉所有内容和设置即可...

2025-11-26 10:03 off999

取消回复欢迎 发表评论: