赞
踩
背景:在有一台服务器上的kafka进程会莫名其妙的消失,暂时没找到原因,放假前决定先临时写一个脚本进行监控,消失了就自动启动一下
目录
1、编写脚本并定义kafka的bin目录及配置文件以及定时任务周期
2、获取当前时间(打印监控日志用)以及根据kafkaBinPath等参数获取当前kafka进程id
1、查询kafka进程id并赋值给变量kafkaPid
2、判断kafkaPid是否为空
为空则启动kafka
不为空则不作任何操作
3、对脚本添加定时任务
- kafkaBinPath='/opt/app/kafka_2.11-2.1.0/bin'
- kafkaConfPath='/opt/app/kafka_2.11-2.1.0/config/server.properties'
- cronTab='* * * * *'
- curDate=`date +"%F %T"`
- kafkaPid=`ps -ef|grep -v grep |grep "${kafkaBinPath}"|grep "server.properties"|awk '{print $2}'`
为空则进入kafkaBinPath目录启动kafka并放入后台执行,等待3秒后查询新进程id并打印,
不为空则直接打印当前进程id
- if [ -z "${kafkaPid}" ];then
- echo -e "kafka is not runing,\nBegin start kafka......"
- cd ${kafkaBinPath}
- nohup ./kafka-server-start.sh -daemon ${kafkaConfPath} 1>/dev/null 2>&1 &
- sleep 3
- newKafkaPid=`ps -ef|grep -v grep |grep "${kafkaBinPath}"|grep "server.properties"|awk '{print $2}'`
- echo "kafkaPid is:${newKafkaPid}"
- else
- echo "kafkaPid is:${kafkaPid}"
- fi
识别脚本目录及文件名,并根据cronTab参数自动添加系统定时任务
- curPath=$(cd `dirname $0`; pwd)
- fileName=`echo "$0"|awk -F/ '{print $NF}'`
- touch /var/spool/cron/root
- sed -i "/${fileName}/d" /var/spool/cron/root
- echo "${cronTab} ${curPath}/${fileName} >> ${curPath}/${fileName}".log >> /var/spool/cron/root
- /sbin/service crond restart $1>/dev/null 2>&1
完整脚本功能实现:脚本通过定义的kafka的bin目录以及配置文件以及定时任务周期来监控kafka进程,如果进程存在则跳过,不存在则启动kafka并将监控日志写入到当前目录下【${fileName}.log】日志文件中,最后将当前文件自动写入到系统任务表中并定时执行
- #!/bin/bash
- ##############################################################
- # File Name:monitr_kafka.sh
- # Version :V1 .
- # Author:
- # Email:
- # Created Time :2023-02-07 15:43:04
- # Description:
- #############################################################
-
- #parameter:Please specify the home directory and configuration file of kafka
- kafkaBinPath='/opt/app/kafka_2.11-2.1.0/bin'
- kafkaConfPath='/opt/app/kafka_2.11-2.1.0/config/server.properties'
- cronTab='* * * * *'
-
-
- curDate=`date +"%F %T"`
- kafkaPid=`ps -ef|grep -v grep |grep "${kafkaBinPath}"|grep "server.properties"|awk '{print $2}'`
-
-
- echo -e "curDate:${curDate}==================================="
- #checking kafka
- if [ -z "${kafkaPid}" ];then
- echo -e "kafka is not runing,\nBegin start kafka......"
- cd ${kafkaBinPath}
- nohup ./kafka-server-start.sh -daemon ${kafkaConfPath} 1>/dev/null 2>&1 &
- sleep 3
- newKafkaPid=`ps -ef|grep -v grep |grep "${kafkaBinPath}"|grep "server.properties"|awk '{print $2}'`
- echo "kafkaPid is:${newKafkaPid}"
- else
- echo "kafkaPid is:${kafkaPid}"
- fi
- echo -e "\n\n"
-
-
- #add crontab
- curPath=$(cd `dirname $0`; pwd)
- fileName=`echo "$0"|awk -F/ '{print $NF}'`
- touch /var/spool/cron/root
- sed -i "/${fileName}/d" /var/spool/cron/root
- echo "${cronTab} ${curPath}/${fileName} >> ${curPath}/${fileName}".log >> /var/spool/cron/root
- /sbin/service crond restart $1>/dev/null 2>&1
[root@localhost ~]# tail -40f /opt/shell/monit_kafka.sh.log
curDate:2023-02-07 17:55:01===================================
zookeeperPid is:14516
kafkaPid is:15107curDate:2023-02-07 18:00:01===================================
kafkaPid is:15107curDate:2023-02-07 20:40:01===================================
kafka is not runing,
Begin start kafka......
kafkaPid is:2815curDate:2023-02-07 20:45:01===================================...
kafkaPid is:2815
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。