当前位置:   article > 正文

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

linux 查看zk进程

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

目录

理论步骤

实现步骤

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

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

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

4、添加系统定时任务

完整脚本

示例


理论步骤

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

     2、判断zookeeperPid是否为空

             为空则启动zookeeper

             不为空则不作任何操作

     3、对脚本添加定时任务

实现步骤

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

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

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

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

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

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

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

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

4、添加系统定时任务

      识别脚本目录及文件名,并根据cronTab参数自动添加系统定时任务

  1. curPath=$(cd `dirname $0`; pwd)
  2. fileName=`echo "$0"|awk -F/ '{print $NF}'`
  3. touch /var/spool/cron/root
  4. sed -i "/${fileName}/d" /var/spool/cron/root
  5. echo "${cronTab} ${curPath}/${fileName} >> ${curPath}/${fileName}".log >> /var/spool/cron/root
  6. /sbin/service crond restart $1>/dev/null 2>&1

完整脚本

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

  1. #!/bin/bash
  2. ##############################################################
  3. # File Name:monitr_zookeeper_kafka.sh
  4. # Version :V1 .
  5. # Author: yelang
  6. # Email: xxxxxx@qq.com
  7. # Created Time :2023-02-07 15:43:04
  8. # Description:
  9. #############################################################
  10. #parameter:Please specify the home directory and configuration file of zk and kafka
  11. zookeeperBinPath='/opt/app/kafka_2.11-2.1.0/bin'
  12. zookeeperConfPath='/opt/app/kafka_2.11-2.1.0/config/zookeeper.properties'
  13. cronTab='* * * * *'
  14. curDate=`date +"%F %T"`
  15. zookeeperPid=`ps -ef|grep -v grep |grep "${zookeeperBinPath}"|grep "zookeeper.properties"|awk '{print $2}'`
  16. echo -e "curDate:${curDate}==================================="
  17. # checking zookeeper
  18. if [ -z "${zookeeperPid}" ];then
  19. echo -e "zookeeper is not runing,\nBegin start zookeeper......"
  20. cd ${zookeeperBinPath}
  21. nohup ./zookeeper-server-start.sh ${zookeeperConfPath} 1>/dev/null 2>&1 &
  22. sleep 1
  23. newZookeeperPid=`ps -ef|grep -v grep |grep "${zookeeperBinPath}"|grep "zookeeper.properties"|awk '{print $2}'`
  24. echo "zookeeperPid is:${newZookeeperPid}"
  25. else
  26. echo "zookeeperPid is:${zookeeperPid}"
  27. fi
  28. echo -e "\n\n"
  29. #add crontab
  30. curPath=$(cd `dirname $0`; pwd)
  31. fileName=`echo "$0"|awk -F/ '{print $NF}'`
  32. touch /var/spool/cron/root
  33. sed -i "/${fileName}/d" /var/spool/cron/root
  34. echo "${cronTab} ${curPath}/${fileName} >> ${curPath}/${fileName}".log >> /var/spool/cron/root
  35. /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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/717012
推荐阅读
相关标签
  

闽ICP备14008679号