logrotate日志轮播

logrotate的配置文件在/etc/logrotate.conf。日志文件的轮续设置在独立的配置文件中。/etc/logrotate.d/

样列一
/var/log/log-file {
    monthly #每月轮续
    rotate 5 #只保留5个log文件,第6个月将删除最早的log文件
    compress #轮续后将自动压缩gzip
    delaycompress #不压缩最近归档,上个月的归档不压缩
    missingok #将log文件中的错误全都过滤掉不保存
    notifempty #如果日志为空,文件将不归档
    create 644 root root #以root身上创建归档,权限0644
    postrotate #执行 killall -HUP rsyslogd命令
        /usr/bin/killall -HUP rsyslogd
    endscript
}
样例二
/var/log/log-file {
    size=50M #只保留一个文件50M大小,超过则开始下一个归档
    rotate 5
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}
样例三
/var/log/log-file {
    monthly
    rotate 5
    dateext #以日期来命名归档文件。eg:Linux-19910825
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

排障

手动
logrotate /etc/logrotate.conf #手动执行所有
logrotate /etc/logrotate.d/yum #手动单个执行
测试

使用-d来测试结果

$>:logrotate -d /etc/logrotate.d/yum

reading config file yum
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /var/log/yum.log  yearly (no old logs will be kept)
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 has been rotated at 2019-5-1 13:0, that is not year ago yet)

logrotate判断小于一天则不执行提醒

强制轮续
$>:logrotate -fv /etc/logtotate.d/yum #-f 强制 -v 详细
定时任务
#!/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 #输出第一行status.clean
sed 's/"//g' status | while read logfile date #替换每行开头的" ,将结果分为logfile 和 date 俩部分 *注①
do
    [ -e "$logfile" ] && echo "\"$logfile\" $date" #如果$logfile存在则输出"$logfile" $date      "/etc/logrotate.d/yum" 1991-0825 
done >> status.clean #输入到文件
mv status.clean status 
 
test -x /usr/sbin/logrotate || exit 0 #test 可执行文件是否存在,不存在就退出,存在就手动logrotate
/usr/sbin/logrotate /etc/logrotate.conf
附① while循环接受
echo 1 2 3  | while read a b c 
do 
echo $a $b $c
done

该学习资料来自Linux中国

你可能感兴趣的:(linux运维,日志管理)