#!/bin/bash
ENV_FILE=~/.bash_profile
# 判断是否有bash_profile文件
if [ ! -f "${ENV_FILE}" ]; then
source ${ENV_FILE}
fi
##java env
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
export PATH=$PATH:$JAVA_HOME/bin
kafka_home_log=/data/SW/kafka/logs
kafka_home=/usr/local/sw/cluster/kafka
startApp="$kafka_home/bin/kafka-server-start.sh -daemon $kafka_home/config/server.properties"
# 停止Kafka服务
function stop(){
echo "stopping kafka"
SIGNAL=${SIGNAL:-TERM}
PIDS=$(ps ax | grep java | grep -i Kafka | grep -v grep | awk '{print $1}')
if [ -z "$PIDS" ]; then
echo "No kafka server to stop"
exit 1
else
kill -s $SIGNAL $PIDS
cleanlog
fi
echo "stoped success"
}
# 启动Kafka服务
function start(){
echo "starting kafka"
# $startApp
COMMAND=$(ps ax | grep java | grep -i Kafka | grep -v grep | awk '{print $1}')
for((i=1;i<=5;i++));
do
if [[ $COMMAND -le 0 ]]; then
$startApp
echo "start $i
sleep 10s
else
echo "process alived"
break
fi
done
echo "started sucess"
}
# 重启kafka服务
function restart(){
echo "restarting kafka"
echo "kafka process is $(ps ax | grep java | grep -i Kafka | grep -v grep | awk '{print $1}')"
stop
sleep 10s
start
echo "kafka process is $(ps ax | grep java | grep -i Kafka | grep -v grep | awk '{print $1}')"
echo "restarted success"
}
function cleanlog(){
echo "删除kafka的临时目录$kafka_home_log"
# 删除kafka的临时目录
rm $kafka_home_log/* -rf
echo "done 删除kafka的临时目录"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
* )
echo "no command"
;;
esac
exit 0