Linux下创建定时任务监控zookeeper进程,异常时自动启动

背景:在有一台服务器上的zookeeper进程会莫名其妙的消失,暂时没找到原因,放假前决定先临时写一个脚本进行监控,消失了就自动启动一下

目录

理论步骤

实现步骤

1、编写脚本并定义zookeeper的bin目录及配置文件以及定时任务周期

2、获取当前时间(打印监控日志用)以及根据zookeeperBinPath等参数获取当前zookeeper进程id

3、判断zookeeperPid参数是否为空,

4、添加系统定时任务

完整脚本

示例


理论步骤

     1、查询zookeeper进程id并赋值给变量zookeeperPid

     2、判断zookeeperPid是否为空

             为空则启动zookeeper

             不为空则不作任何操作

     3、对脚本添加定时任务

实现步骤

1、编写脚本并定义zookeeper的bin目录及配置文件以及定时任务周期

zookeeperBinPath='/opt/app/kafka_2.11-2.1.0/bin'
zookeeperConfPath='/opt/app/kafka_2.11-2.1.0/config/zookeeper.properties'
cronTab='* * * * *'

2、获取当前时间(打印监控日志用)以及根据zookeeperBinPath等参数获取当前zookeeper进程id

curDate=`date +"%F %T"`
zookeeperPid=`ps -ef|grep -v grep |grep "${zookeeperBinPath}"|grep "zookeeper.properties"|awk '{print $2}'`

3、判断zookeeperPid参数是否为空,

     为空则进入zookeeperBinPath目录启动kafka并放入后台执行,等待3秒后查询新进程id并打印,

      不为空则直接打印当前进程id

if [ -z "${zookeeperPid}" ];then
  echo -e "zookeeper is not runing,\nBegin start zookeeper......"
  cd ${zookeeperBinPath}
  nohup ./zookeeper-server-start.sh ${zookeeperConfPath} 1>/dev/null 2>&1 &
 sleep 1
  newZookeeperPid=`ps -ef|grep -v grep |grep "${zookeeperBinPath}"|grep "zookeeper.properties"|awk '{print $2}'` 
  echo "zookeeperPid is:${newZookeeperPid}"
else
  echo "zookeeperPid is:${zookeeperPid}"
fi

4、添加系统定时任务

      识别脚本目录及文件名,并根据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

完整脚本

完整脚本功能实现:脚本通过定义的zookeeper的bin目录以及配置文件以及定时任务周期来监控zookeeper进程,如果进程存在则跳过,不存在则启动zookeeper并将监控日志写入到当前目录下【以当前文件名开始以.log】的日志文件中,最后将当前文件自动写入到系统任务表中并定时执行

#!/bin/bash
##############################################################
# File Name:monitr_zookeeper_kafka.sh
# Version :V1 . 
# Author: yelang
# Email: [email protected]
# Created Time :2023-02-07 15:43:04
# Description:
#############################################################

#parameter:Please specify the home directory and configuration file of zk and kafka 
zookeeperBinPath='/opt/app/kafka_2.11-2.1.0/bin'
zookeeperConfPath='/opt/app/kafka_2.11-2.1.0/config/zookeeper.properties'
cronTab='* * * * *'


curDate=`date +"%F %T"`
zookeeperPid=`ps -ef|grep -v grep |grep "${zookeeperBinPath}"|grep "zookeeper.properties"|awk '{print $2}'`


echo -e "curDate:${curDate}==================================="
# checking zookeeper
if [ -z "${zookeeperPid}" ];then
  echo -e "zookeeper is not runing,\nBegin start zookeeper......"
  cd ${zookeeperBinPath}
  nohup ./zookeeper-server-start.sh ${zookeeperConfPath} 1>/dev/null 2>&1 &
 sleep 1
  newZookeeperPid=`ps -ef|grep -v grep |grep "${zookeeperBinPath}"|grep "zookeeper.properties"|awk '{print $2}'` 
  echo "zookeeperPid is:${newZookeeperPid}"
else
  echo "zookeeperPid is:${zookeeperPid}"
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/monitr_zookeeper.sh.log 

curDate:2023-02-07 17:50:01===================================
zookeeperPid is:14516
 

curDate:2023-02-07 17:55:01===================================
zookeeperPid is:14516
 

curDate:2023-02-07 18:00:01===================================
zookeeperPid is:14516
 

curDate:2023-02-07 20:40:01===================================
zookeeper is not runing,
Begin start zookeeper......
zookeeperPid is:2233
 

curDate:2023-02-07 20:45:01===================================
zookeeperPid is:2233

你可能感兴趣的:(linux,shell,zookeeper,linux,运维,zookeeper,centos)