Linux 使用logrotate 对日志切片

执行命令

vi /etc/logrotate.d/tomcat 

添加内容

#daily 不要加 !! 不然会和linux自带的定时任务重复

//olddir 不会自动创建文件夹,需要手动创建

/usr/software/apache-tomcat-8.5.45/logs/catalina.out {
copytruncate
rotate 365
dateext
nocompress
missingok
ifempty
olddir /usr/software/apache-tomcat-8.5.45/catalina-logs
}

配置完后执行一次测试

/usr/sbin/logrotate -f /etc/logrotate.conf 

如果报这个错 在上面的一大堆里面在添加

error: skipping “/usr/local/tomcat8/logs/catalina.out” because parent directory has insecure permissions (It’s world writable or writable by group which is not “root”) Set “su” directive in config file to tell logrotate which user/group should be used for rotation.

su root root 

什么是logrotate

logrotate 程序是一个日志文件管理工具。用于分割日志文件,删除旧的日志文件,并创建新的日志文件,起到“转储”作用。可以节省磁盘空间。

logrotate文件位置

该配置文件位于,/etc/logrotate.conf,/etc/logrotate.d/。
还有crontab -e生成的临时文件 在/tmp

常见配置参数

在这里插入图片描述
dateext                                                                    日期作为后缀

配置linux自带系统定时器

#脚本写到定时计划中去(每天2359分运⾏)
[root@localhost etc]# crontab -e
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/tomcat

#查看当前定时任务列表
[root@localhost etc]# crontab -l
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/tomcat

重新载入定时配置,启动定时器,查看定时状态

#重启载⼊配置
[root@localhost etc]# systemctl reload crond
#如未启动,则需要启动crontab
[root@localhost etc]# systemctl start cr
[root@localhost etc]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 二 2021-04-06 16:13:43 CST; 4 months 24 days ago
  Process: 1533 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 892 (crond)
    Tasks: 1
   Memory: 1.0M
   CGroup: /system.slice/crond.service
           └─892 /usr/sbin/crond -n


[root@localhost etc]# systemctl list-unit-files
UNIT FILE                                     STATE   
proc-sys-fs-binfmt_misc.automount             static  
dev-hugepages.mount                           static  
dev-mqueue.mount                              static  
proc-sys-fs-binfmt_misc.mount                 static  
run-vmblock\x2dfuse.mount                     disabled
sys-fs-fuse-connections.mount                 static  
sys-kernel-config.mount                       static  
sys-kernel-debug.mount                        static  
tmp.mount                                     disabled
brandbot.path                                 disabled
systemd-ask-password-console.path             static  
systemd-ask-password-plymouth.path            static  
systemd-ask-password-wall.path                static  
session-1029.scope                            static  
session-1049.scope                            static  
session-2783.scope                            static  
session-2784.scope                            static  
session-3581.scope                            static  
arp-ethers.service                            disabled
auditd.service                                enabled 
autovt@.service                               enabled 
blk-availability.service                      disabled
brandbot.service                              static  
chrony-dnssrv@.service                        static  
chrony-wait.service                           disabled

#已启动
chronyd.service                               enabled 
console-getty.service                         disabled
console-shell.service                         disabled
container-getty@.service                      static  
........

配置完成后发现每天执行两次 找了半天看日志发现是anacrontab导致的
将配置文件的START_HOURS_RANGE修改为符合的时间段例如23-24

详细查看:anacrontab

http://c.biancheng.net/view/1095.html
/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
  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值