linux logrotate日志轮询设置案例二

本文介绍了如何在Linux系统中设置logrotate进行日志轮换,包括编辑`/etc/logrotate.conf`文件添加配置,设置转储频率、权限及脚本执行,以及如何使用logrotate命令进行测试。同时,文章提到了`/var/lib/logrotate/logrotate.status`文件用于记录日志转储的最新时间,以及`/etc/anacrontab`文件中的设定影响logrotate的执行时机。
摘要由CSDN通过智能技术生成

1.编辑/etc/logrotate.conf文件,添加如下配置,并保存

/var/log/ztj.log {
    missingok
    daily 
    create 644 root root      
    sharedscripts
    postrotate
    if [ -f /var/run/syslogd.pid ];then
    /bin/kill -HUP $(/bin/cat /var/run/syslogd.pid) >/dev/null 2>&1 || true
    fi
    endscript
}

/var/log/ztj.log {  #转储日志路径
    missingok      #当日志文件不存在时,忽略该日志文件的警告信息
    daily            #每天转储一次
    create 644 root root   #指定转储后创建新文件,权限为644,用户为root,属组为root
    sharedscripts   #该关键字后的脚本只执行一次
    postrotate     #在日志轮替之后执行脚本命令
    if [ -f /var/run/syslogd.pid ];then
    /bin/kill -HUP $(/bin/cat /var/run/syslogd.pid) >/dev/null 2>&1 || true   #重启syslogd应用
    fi
    endscript        #在日志轮替之后执行脚本命令
}

2.测试

由于日志转储设置为了daily,如果我们想及时知晓结果,可以使用logrotate命令手动测试。

logrotate命令用法:

logrotate [参数] /etc/logrotate.conf

logrotate参数
参数说明
-d打开调试模式。在调试模式下,不会对日志或文件进行任何更改,即:不会真的切割日志
-v详细显示输出信息
-f强制轮转

-->debug测试是否生效,不会真的切割日志

命令:

logrotate -dvf /etc/logrotate.conf

[root@rhel77 log]# logrotate -dvf /etc/logrotate.conf 
reading config file /etc/logrotate.conf
including /etc/logrotate.d
reading config file bootlog
reading config file chrony
reading config file cups
reading config file httpd
reading config file iscsiuiolog
reading config file libvirtd
reading config file libvirtd.lxc
reading config file libvirtd.qemu
reading config file mysql
reading config file nginx
reading config file numad
reading config file ppp
reading config file psacct
reading config file samba
olddir is now /var/log/samba/old
reading config file subscription-manager
reading config file syslog
reading config file up2date
reading config file wpa_supplicant
reading config file yum
Allocating hash table for state file, size 15360 B

Handling 21 logs

rotating pattern: /var/log/boot.log
 forced from command line (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/boot.log
  log does not need rotating (log is empty)
rotating pattern: /var/log/chrony/*.log  forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/chrony/*.log
  log /var/log/chrony/*.log does not exist -- skipping
not running postrotate script, since no logs were rotated

rotating pattern: /var/log/cups/*_log  forced from command line (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/cups/access_log
  log does not need rotating (log is empty)considering log /var/log/cups/error_log
  log does not need rotating (log is empty)considering log /var/log/cups/page_log
  log does not need rotating (log is empty)
rotating pattern: /var/log/httpd/*log  forced from command line (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/httpd/access_log
  log does not need rotating (log is empty)considering log /var/log/httpd/error_log
  log does not need rotating (log is empty)not running postrotate script, since no logs were rotated

rotating pattern: /var/log/iscsiuio.log  forced from command line (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/iscsiuio.log
  log /var/log/iscsiuio.log does not exist -- skipping
not running postrotate script, since no logs were rotated

rotating pattern: /var/log/libvirt/libvirtd.log  forced from command line (4 rotations)
empty log files are rotated, only log files >= 102400 bytes are rotated, old logs are removed
considering log /var/log/libvirt/libvirtd.log
  log /var/log/libvirt/libvirtd.log does not exist -- skipping

rotating pattern: /var/log/libvirt/lxc/*.log  forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/libvirt/lxc/*.log
  log /var/log/libvirt/lxc/*.log does not exist -- skipping

rotating pattern: /var/log/libvirt/qemu/*.log  forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/libvirt/qemu/*.log
  log /var/log/libvirt/qemu/*.log does not exist -- skipping

rotating pattern: /var/log/nginx/*.log  forced from command line (52 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  log does not need rotating (log is empty)considering log /var/log/nginx/error.log
  log does not need rotating (log is empty)not running postrotate script, since no logs were rotated

rotating pattern: /var/log/numad.log  forced from command line (5 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/numad.log
  log /var/log/numad.log does not exist -- skipping

rotating pattern: /var/log/ppp/connect-errors  forced from command line (5 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/ppp/connect-errors
  log /var/log/ppp/connect-errors does not exist -- skipping

rotating pattern: /var/account/pacct  forced from command line (31 rotations)
empty log files are not rotated, old logs are removed
considering log /var/account/pacct
error: stat of /var/account/pacct failed: No such file or directory

rotating pattern: /var/log/samba/*  forced from command line (4 rotations)
olddir is /var/log/samba/old, empty log files are not rotated, old logs are removed
No logs found. Rotation not needed.

rotating pattern: /var/log/rhsm/*.log  forced from command line (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/rhsm/rhsmcertd.log
  log does not need rotating (log is empty)considering log /var/log/rhsm/rhsm.log
  log does not need rotating (log is empty)
rotating pattern: /var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
 forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/cron
  log needs rotating
considering log /var/log/maillog
  log needs rotating
considering log /var/log/messages
  log needs rotating
considering log /var/log/secure
  log needs rotating
considering log /var/log/spooler
  log /var/log/spooler does not exist -- skipping
rotating log /var/log/cron, log->rotateCount is 4
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/cron-20231117 already exists, skipping rotation
rotating log /var/log/maillog, log->rotateCount is 4
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/maillog-20231117 already exists, skipping rotation
rotating log /var/log/messages, log->rotateCount is 4
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/messages-20231117 already exists, skipping rotation
rotating log /var/log/secure, log->rotateCount is 4
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/secure-20231117 already exists, skipping rotation

rotating pattern: /var/log/up2date  forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/up2date
  log /var/log/up2date does not exist -- skipping

rotating pattern: /var/log/wpa_supplicant.log  forced from command line (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/wpa_supplicant.log
  log /var/log/wpa_supplicant.log does not exist -- skipping

rotating pattern: /var/log/yum.log  forced from command line (4 rotations)
empty log files are not rotated, log files >= 30720 are rotated earlier, old logs are removed
considering log /var/log/yum.log
  log does not need rotating (log is empty)
rotating pattern: /var/log/wtmp  forced from command line (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /var/log/wtmp
  log needs rotating
rotating log /var/log/wtmp, log->rotateCount is 1
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/wtmp-20231117 already exists, skipping rotation

rotating pattern: /var/log/btmp  forced from command line (1 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/btmp
  log needs rotating
rotating log /var/log/btmp, log->rotateCount is 1
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/btmp-20231117 already exists, skipping rotation

rotating pattern: /var/log/ztj.log  forced from command line (4 rotations)
empty log files are rotated, old logs mailed to 654806827@qq.com
considering log /var/log/ztj.log
  log needs rotating
rotating log /var/log/ztj.log, log->rotateCount is 4
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /var/log/ztj.log to /var/log/ztj.log-20231117
creating new /var/log/ztj.log mode = 0644 uid = 0 gid = 0
running postrotate script
running script with arg /var/log/ztj.log : "
    if [ -f /var/run/syslogd.pid ];then
    /bin/kill -HUP $(/bin/cat /var/run/syslogd.pid) >/dev/null 2>&1 || true
    fi
"
[root@rhel77 log]# 

-->强制生效并显示详细的输出信息

命令:

logrotate -vf /etc/logrotate.conf

[root@rhel77 log]# logrotate -vf /etc/logrotate.conf 
reading config file /etc/logrotate.conf
including /etc/logrotate.d
reading config file bootlog
reading config file chrony
reading config file cups
reading config file httpd
reading config file iscsiuiolog
reading config file libvirtd
reading config file libvirtd.lxc
reading config file libvirtd.qemu
reading config file mysql
reading config file nginx
reading config file numad
reading config file ppp
reading config file psacct
reading config file samba
olddir is now /var/log/samba/old
reading config file subscription-manager
reading config file syslog
reading config file up2date
reading config file wpa_supplicant
reading config file yum
Allocating hash table for state file, size 15360 B

Handling 21 logs

rotating pattern: /var/log/boot.log
 forced from command line (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/boot.log
  log does not need rotating (log is empty)
rotating pattern: /var/log/chrony/*.log  forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/chrony/*.log
  log /var/log/chrony/*.log does not exist -- skipping
not running postrotate script, since no logs were rotated

rotating pattern: /var/log/cups/*_log  forced from command line (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/cups/access_log
  log does not need rotating (log is empty)considering log /var/log/cups/error_log
  log does not need rotating (log is empty)considering log /var/log/cups/page_log
  log does not need rotating (log is empty)
rotating pattern: /var/log/httpd/*log  forced from command line (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/httpd/access_log
  log does not need rotating (log is empty)considering log /var/log/httpd/error_log
  log does not need rotating (log is empty)not running postrotate script, since no logs were rotated

rotating pattern: /var/log/iscsiuio.log  forced from command line (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/iscsiuio.log
  log /var/log/iscsiuio.log does not exist -- skipping
not running postrotate script, since no logs were rotated

rotating pattern: /var/log/libvirt/libvirtd.log  forced from command line (4 rotations)
empty log files are rotated, only log files >= 102400 bytes are rotated, old logs are removed
considering log /var/log/libvirt/libvirtd.log
  log /var/log/libvirt/libvirtd.log does not exist -- skipping

rotating pattern: /var/log/libvirt/lxc/*.log  forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/libvirt/lxc/*.log
  log /var/log/libvirt/lxc/*.log does not exist -- skipping

rotating pattern: /var/log/libvirt/qemu/*.log  forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/libvirt/qemu/*.log
  log /var/log/libvirt/qemu/*.log does not exist -- skipping

rotating pattern: /var/log/nginx/*.log  forced from command line (52 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  log does not need rotating (log is empty)considering log /var/log/nginx/error.log
  log does not need rotating (log is empty)not running postrotate script, since no logs were rotated

rotating pattern: /var/log/numad.log  forced from command line (5 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/numad.log
  log /var/log/numad.log does not exist -- skipping

rotating pattern: /var/log/ppp/connect-errors  forced from command line (5 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/ppp/connect-errors
  log /var/log/ppp/connect-errors does not exist -- skipping

rotating pattern: /var/account/pacct  forced from command line (31 rotations)
empty log files are not rotated, old logs are removed
considering log /var/account/pacct
error: stat of /var/account/pacct failed: No such file or directory

rotating pattern: /var/log/samba/*  forced from command line (4 rotations)
olddir is /var/log/samba/old, empty log files are not rotated, old logs are removed
No logs found. Rotation not needed.

rotating pattern: /var/log/rhsm/*.log  forced from command line (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/rhsm/rhsmcertd.log
  log does not need rotating (log is empty)considering log /var/log/rhsm/rhsm.log
  log does not need rotating (log is empty)
rotating pattern: /var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
 forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/cron
  log needs rotating
considering log /var/log/maillog
  log needs rotating
considering log /var/log/messages
  log needs rotating
considering log /var/log/secure
  log needs rotating
considering log /var/log/spooler
  log /var/log/spooler does not exist -- skipping
rotating log /var/log/cron, log->rotateCount is 4
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/cron-20231117 already exists, skipping rotation
rotating log /var/log/maillog, log->rotateCount is 4
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/maillog-20231117 already exists, skipping rotation
rotating log /var/log/messages, log->rotateCount is 4
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/messages-20231117 already exists, skipping rotation
rotating log /var/log/secure, log->rotateCount is 4
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/secure-20231117 already exists, skipping rotation

rotating pattern: /var/log/up2date  forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/up2date
  log /var/log/up2date does not exist -- skipping

rotating pattern: /var/log/wpa_supplicant.log  forced from command line (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/wpa_supplicant.log
  log /var/log/wpa_supplicant.log does not exist -- skipping

rotating pattern: /var/log/yum.log  forced from command line (4 rotations)
empty log files are not rotated, log files >= 30720 are rotated earlier, old logs are removed
considering log /var/log/yum.log
  log does not need rotating (log is empty)
rotating pattern: /var/log/wtmp  forced from command line (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /var/log/wtmp
  log needs rotating
rotating log /var/log/wtmp, log->rotateCount is 1
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/wtmp-20231117 already exists, skipping rotation

rotating pattern: /var/log/btmp  forced from command line (1 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/btmp
  log needs rotating
rotating log /var/log/btmp, log->rotateCount is 1
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
destination /var/log/btmp-20231117 already exists, skipping rotation

rotating pattern: /var/log/ztj.log  forced from command line (4 rotations)
empty log files are rotated, old logs mailed to 654806827@qq.com
considering log /var/log/ztj.log
  log needs rotating
rotating log /var/log/ztj.log, log->rotateCount is 4
dateext suffix '-20231117'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /var/log/ztj.log to /var/log/ztj.log-20231117
creating new /var/log/ztj.log mode = 0644 uid = 0 gid = 0
running postrotate script
[root@rhel77 log]# ls -l ztj.log-20231117
-rw-r--r-- 1 root root 679 Nov 17 09:32 ztj.log-20231117
[root@rhel77 log]# 

补充:

每天定时执行/etc/cron.daily目录下的文件设置,其是在/etc/anacrontab文件定义的,那么anacrontab怎么知道上次成功执行脚本的具体时间呢?通过查看/etc/cron.daily/logrotate文件,我们知晓/etc/cron.daily/logrotate文件中记录了/var/lib/logrotate/logrotate.status这个一个文件,此文件的作用是记录最近一次成功运行日志转储的具体时间。

-->/var/lib/logrotate/logrotate.status文件详情

[root@rhel77 ~]# cat /var/lib/logrotate/logrotate.status 
logrotate state -- version 2
"/var/log/nginx/error.log" 2023-11-17-9:1:1
"/var/log/yum.log" 2023-11-8-14:52:21
"/var/log/cups/page_log" 2023-2-24-15:0:0
"/var/log/cups/error_log" 2023-2-24-15:0:0
"/var/log/boot.log" 2023-11-17-9:1:1
"/var/log/up2date" 2023-1-31-16:0:0
"/var/log/cups/access_log" 2023-2-24-15:0:0
"/var/log/libvirt/lxc/*.log" 2023-2-24-15:0:0
"/var/log/httpd/error_log" 2023-10-19-10:34:1
"/var/log/wtmp" 2023-11-17-9:35:5
"/var/log/chrony/*.log" 2023-1-31-16:0:0
"/var/log/spooler" 2023-5-21-14:37:1
"/var/log/btmp" 2023-11-17-9:35:5
"/var/log/rhsm/rhsmcertd.log" 2023-11-17-9:20:33
"/var/log/iscsiuio.log" 2023-1-31-16:0:0
"/var/log/maillog" 2023-11-17-9:35:5
"/var/log/libvirt/libvirtd.log" 2023-2-24-15:0:0
"/var/log/libvirt/qemu/*.log" 2023-2-24-15:0:0
"/var/log/ztj.log" 2023-11-17-9:35:5
"/var/log/wpa_supplicant.log" 2023-1-31-16:0:0
"/var/log/secure" 2023-11-17-9:35:5
"/var/log/numad.log" 2023-2-24-15:0:0
"/var/log/nginx/access.log" 2023-11-14-9:1:1
"/var/log/rhsm/rhsm.log" 2023-11-17-9:20:33
"/var/log/ppp/connect-errors" 2023-2-24-15:0:0
"/var/log/messages" 2023-11-17-9:35:5
"/var/log/httpd/access_log" 2023-6-4-10:30:1
"/var/log/cron" 2023-11-17-9:35:5
"/var/account/pacct" 2023-2-24-15:0:0
[root@rhel77 ~]# 

-->/etc/anacrontab文件详情

[root@rhel77 ~]# cat /etc/anacrontab 
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1	5	cron.daily		nice run-parts /etc/cron.daily
7	25	cron.weekly		nice run-parts /etc/cron.weekly
@monthly 45	cron.monthly		nice run-parts /etc/cron.monthly
[root@rhel77 ~]# 

 基于/etc/anacrontab文件,如果机器没有关机/异常,默认logrotate每天会在3:05-3:50之间执行,其真实的延迟时间是RANDOM_DELAY + delay in minutes。

如果在3点-22点(START_HOURS_RANGE)期间,服务器处于关机状态,则logrotate会在机器开机5分钟后执行日志转储操作。

  • 30
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑要上天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值