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

Posted 夜灬狼丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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: xxxxxx@qq.com
# 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下创建定时任务监控zookeeper进程,异常时自动启动的主要内容,如果未能解决你的问题,请参考以下文章

linux定时任务crontab的使用

Linux crond任务调度 磁盘分区和挂载 网络环境 进程管理 服务(service)管理 动态监控进程 rpm和yum

SparkStreaming任务保持运行,定时任务监控进程,保证不挂掉

Linux下通过crontab命令来实现定时任务

linux定时任务

linux定时执行任务