1:在目錄 /usr/local/bin下 touch xzk.sh
2: chmod 777 xzk.sh
3: 編輯 xzk.sh
#判斷用戶是否傳參 if [ $# -ne 1 ];then echo "無效參數,用法為: $0 {start|stop|restart|status}" exit fi #獲取用戶輸入的命令 cmd=$1 #定義函數功能 function zookeeperManger(){ case $cmd in start) echo "啟動服務" remoteExecution start ;; stop) echo "停止服務" remoteExecution stop ;; restart) echo "重啟服務" remoteExecution restart ;; status) echo "查看狀態" remoteExecution status ;; *) echo "無效參數,用法為: $0 {start|stop|restart|status}" ;; esac } #定義執行的命令 function remoteExecution(){ for (( i=202 ; i<=204 ; i++ )) ; do tput setaf 2 echo ========== hadoop${i} zkServer.sh $1 ================ tput setaf 9 ssh hadoop${i} "source /etc/profile ; /opt/module/zookeeper-3.4.10/bin/zkServer.sh $1" done } #調用函數 zookeeperManger