1.我腳本放在/root/bin下面,命名kafka-mgr.sh
2.root目錄下面沒有bin目錄就創建 mkdir /root/bin -p
3.創建kafka-mgr.sh腳本,vim kafka-mgr.sh(沒有vim可以安裝,也可以用vi,vim安裝yum -y install vim*)
4.編寫shell腳本
#!/bin/bash
echo $1"ing kafkaServer ..."
for i in 2 3 4 5
do
echo "centos10"$i" is "$1"ing..."
if [ $1 = "start" ];then
ssh centos10$i "source /etc/profile;/home/kafka_2.12-2.3.0/bin/kafka-server-"$1".sh -daemon /home/kafka_2.12-2.3.0/config/server.properties"
elif [ $1 = "stop" ];then
ssh centos10$i "source /etc/profile;/home/kafka_2.12-2.3.0/bin/kafka-server-"$1".sh"
else
echo "the command is wrong!!!"
fi
done
5.爲了方便導出都可以啓動,可以把/root/bin加入環境變量
6.source /etc/profile
7.啓動kafka-mgr.sh start
8.停止kafka-mgr.sh stop