kafka 启停脚本

本文详细介绍了如何在Linux环境中编写和使用Kafka的启动与停止脚本,包括配置环境变量、启动脚本的编写逻辑、停止服务的方法,以及在实际操作中可能遇到的问题和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash
zkPort=2181
kaPort=9092
kaFile='/usr/local/kafka/config/server.properties'
zkFile='/usr/local/kafka/config/zookeeper.properties'
logsDir='/data/logs'
blue() {
	echo -e "\033[34m $1  \033[0m" && sleep 1
}

red() {
    echo -e "\033[31m $1  \033[0m" && sleep 1
}


function start_zk() {
    zknum=$(ss -tunlp|grep -w ${zkPort}|wc -l)
    kanum=$(ss -tunlp|grep -w ${kaPort}|wc -l)
    if [ $zknum -eq 0 ] && [ $kanum -eq 0 ];then
        nohup zookeeper-server-start.sh ${zkFile} > ${logsDir}/zk-`date +%y%m%d-%H%M`.log 2>&1 &
        #tail -f  zk-`date +%y%m%d-%H%M`.log
    fi

    while true; do
      zknum=$(ss -tunlp|grep -w ${zkPort}|wc -l)
      if [ "$zknum" = "0" ]; then
        blue "The zookeeper  process is starting, it may take some time, please wait patiently..."
        sleep 1
      else
        red "zookeeper start successfully!"
        break
      fi
    done

}

function start_ka() {
    zknum=$(ss -tunlp|grep -w ${zkPort}|wc -l)
    if [ "$zknum" = 1 ];then 
        nohup kafka-server-start.sh ${kaFile} > ${logsDir}/kafka-`date +%y%m%d-%H%M`.log 2>&1 &
        #tail -f  kafka-`date +%y%m%d-%H%M`.log
    else
        #echo "请先启动zookeeper!"
        red "请先启动zookeeper"
        exit 0
    fi

    while true; do
      kanum=$(ss -tunlp|grep -w ${kaPort}|wc -l)
      if [ "$kanum" = "0" ]; then
        blue "The kafka  process is starting, it may take some time,please wait patiently..."
        sleep 1
      else
        red "kafka start successfully!"
        break
      fi
    done
}

function stop_ka() {
    kanum=$(ss -tunlp|grep -w ${kaPort}|wc -l)
    if [ "$kanum" = "1" ]; then
        kafka-server-stop.sh ${kaFile} &
    fi
    
    while true; do
      kanum=$(ss -tunlp|grep -w ${kaPort}|wc -l)
      if [ "$kanum" = "1" ]; then
        #echo "The kafka  process is exiting, it may take some time, forcing the exit may cause damage to the database, please wait patiently..."
        blue "The kafka  process is exiting, it may take some time, forcing the exit may cause damage to the database, please wait patiently..."
        sleep 1
      else
        #echo "kafka stop successfully!"
        red "kafka stop successfully!"
        break
      fi
    done
}

function stop_zk() {
    kanum=$(ss -tunlp|grep -w ${kaPort}|wc -l)
    if [ "$kanum" = "1" ]; then
        #echo "请先停止kafka!"
        red "请先停止kafka!"
        exit 0
    fi

    zknum=$(ss -tunlp|grep -w ${zkPort}|wc -l)
    if [ "$zknum" = "1" ]; then
       zookeeper-server-stop.sh ${zkFile} &
    fi

    while true; do
      kanum=$(ss -tunlp|grep -w ${zkPort}|wc -l)
      if [ "$kanum" = "1" ]; then
        #echo "The zookeeper  process is exiting, it may take some time, forcing the exit may cause damage to the database, please wait patiently..."
        blue "The zookeeper  process is exiting, it may take some time, forcing the exit may cause damage to the database, please wait patiently..."
        sleep 1
      else
        #echo "zookeeper stop successfully!"
        red "zookeeper stop successfully!"
        break
      fi
    done
}

#start_zk
#start_ka
#stop_ka
#stop_zk

cat << EOF
+-------【kafka启停】---------+
|1、 启动zookeeper            |
|2、 启动kafka                |
|3、 停止kafka                |
|4、 停止zookeeper            |
|5、 顺序启动zookeeper、kafka |
|6、 顺序停止kafka、zookeeper||
+============================-+
|[Q|q|quit] to quit           |
+-----------------------------+
EOF

echo -ne "\033[32m -->【请选择你要执行的选项编号!(1|2|3)】: \033[0m"
read choice

case $choice in
    1)
      start_zk
      #tail -f  zk-`date +%y%m%d-%H%M`.log
      ;;
    2)
      start_ka
      ;;
    3)
      stop_ka
      ;;
    4)
      stop_zk
      ;;
    5)
      start_zk
      start_ka
      ;;
    6)
      stop_ka
      stop_zk
      ;;      
    Q|q|quit)
      exit
      ;;
    *)
      echo "程序异常退出,Please: select one number(1|2|3)"
      exit
      ;;
esac

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周易不易

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值