本文主要是介绍定时清理rocketmq日志--crontab,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、背景
之前在部署rocketmq的时候未修改日志路径,导致在用户目录下有日志数据写入。因不方便修改或空间足够可正常写入,但日志量过大需清理,现添加定时任务执行。
2、规划:
目前测试阶段,所以时间是可变的,后面可以根据实际需求修改时间。
用户:root
日志路径:/root/logs/rocketmqlogs/
定时任务脚本存放路径:/root/logs/deletelog.sh
3、脚本编写+定时任务添加
3.1脚本编写
cd /root/logs
vi deletelog.sh
#!/bin/bash
source /etc/profile
#指定路径
log_path="/root/logs/rocketmqlogs/"
max_size=100M
#max_days=90
#获取指定路径下的所有日志文件
#log_files=$(find $log_path -type f \( -size +$max_size -o -mtime +$max_days \))
log_files=$(find $log_path -type f \( -size +$max_size \))
echo "123"
##遍历每个日志文件
for file in $log_files;do
echo "$file"
#echo "234"
rm "$file"
echo "File #file has been deleted as it exceeds 100MB or existing for more than 3 days"
echo "已执行日志清理脚本"
done
3.2定时任务添加
如果不知道定时怎么写,可以看看
[root@localhost mail]# vi /etc/crontab
设置定时任务:crontab -e
49 16 * * * /root/logs/deletelog.sh
查看定时任务是否建成:crontab -l
[root@localhost mail]# crontab -e
crontab: installing new crontab
[root@localhost mail]# crontab -l
49 16 * * * /root/logs/deletelog.sh[root@localhost mail]# ll /root/logs/
total 8
-rwxr-xr-x 1 root root 524 Jun 13 16:35 deletelog.sh
drwxr-xr-x 3 root root 4096 Jun 13 16:49 rocketmqlogs
You have new mail in /var/spool/mail/root
4、问题
日志未清理-红框中的内容还存在
4.1查看定时任务执行时报错–/bin/sh: root: command not found
vi /var/spool/mail/root
[root@localhost mail]# vi /var/spool/mail/root
From root@localhost.localdomain Thu Jun 13 15:50:02 2024
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)id 02AC92DB77E; Thu, 13 Jun 2024 15:50:01 +0800 (CST)
From: "(Cron Daemon)" <root@localhost.localdomain>
To: root@localhost.localdomain
Subject: Cron <root@localhost> root /root/logs/deletelog.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=119333>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20240613075002.02AC92DB77E@localhost.localdomain>
Date: Thu, 13 Jun 2024 15:50:01 +0800 (CST)/bin/sh: root: command not found
此处是因为一开始我的脚本中没有写:#!/bin/bash,所以定时任务无法执行。
4.2查看定时任务执行时报错–/bin/sh: /root/logs/deletelog.sh: Permission denied
From root@localhost.localdomain Thu Jun 13 16:39:01 2024
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)id 225FB2DB77C; Thu, 13 Jun 2024 16:39:01 +0800 (CST)
From: "(Cron Daemon)" <root@localhost.localdomain>
To: root@localhost.localdomain
Subject: Cron <root@localhost> /root/logs/deletelog.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=119344>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20240613083901.225FB2DB77C@localhost.localdomain>
Date: Thu, 13 Jun 2024 16:39:01 +0800 (CST)/bin/sh: /root/logs/deletelog.sh: Permission denied
此处是脚本所在目录的权限不够,无法执行,后赋予755后好了。
终于正确执行,日志打印如下
From root@localhost.localdomain Thu Jun 13 16:49:01 2024
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)id 46BFD2DB77E; Thu, 13 Jun 2024 16:49:01 +0800 (CST)
From: "(Cron Daemon)" <root@localhost.localdomain>
To: root@localhost.localdomain
Subject: Cron <root@localhost> /root/logs/deletelog.sh
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=119346>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20240613084901.46BFD2DB77E@localhost.localdomain>
Date: Thu, 13 Jun 2024 16:49:01 +0800 (CST)123
/root/logs/rocketmqlogs/rocketmq_client.log.9
File #file has been deleted as it exceeds 100MB or existing for more than 3 days
已执行日志清理脚本
/root/logs/rocketmqlogs/rocketmq_client.log.7
File #file has been deleted as it exceeds 100MB or existing for more than 3 days
已执行日志清理脚本
/root/logs/rocketmqlogs/rocketmq_client.log.5
File #file has been deleted as it exceeds 100MB or existing for more than 3 days
已执行日志清理脚本
/root/logs/rocketmqlogs/rocketmq_client.log.3
File #file has been deleted as it exceeds 100MB or existing for more than 3 days
已执行日志清理脚本
/root/logs/rocketmqlogs/rocketmq_client.log.10
File #file has been deleted as it exceeds 100MB or existing for more than 3 days
已执行日志清理脚本
/root/logs/rocketmqlogs/rocketmq_client.log.8
File #file has been deleted as it exceeds 100MB or existing for more than 3 days
已执行日志清理脚本
/root/logs/rocketmqlogs/rocketmq_client.log.6
File #file has been deleted as it exceeds 100MB or existing for more than 3 days
已执行日志清理脚本
/root/logs/rocketmqlogs/rocketmq_client.log.4
File #file has been deleted as it exceeds 100MB or existing for more than 3 days
已执行日志清理脚本
/root/logs/rocketmqlogs/rocketmq_client.log.2
File #file has been deleted as it exceeds 100MB or existing for more than 3 days
已执行日志清理脚本
5、小结
- 编写脚本时一定要记得加#!/bin/bash。
- 编写脚本后一定要查看是否有执行权限。
- 如果还是执行不起来,注意是否环境变量掉了。【可以写到脚本里,每次执行时加载环境变量】
6、链接
如何更改日志路径,以下链接供您参考,5.0.0版本及5.1.2版本。
链接: rocketmq-5.0.0的dleger高可用集群部署
链接: rocketmq-5.1.2的dleger高可用集群部署
这篇关于定时清理rocketmq日志--crontab的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!