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