脚本很简单内容如下:
age="Usage: $0 (start|stop|status)" if [ $# -lt 1 ]; then echo $usage exit 1 fi behave=$1 echo "$behave zkServer cluster" for i in hadoop01 hadoop02 hadoop03 do ssh $i "/home/software/zookeeper-3.4.7/bin/zkServer.sh $behave" done exit 0
原因: 首先需要知道 交互式shell和非交互式shell、登录shell和非登录shell是有区别的 在登录shell里,环境信息需要读取/etc/profile和~ /.bash_profile, ~/.bash_login, and ~/.profile按顺序最先的一个,并执行其中的命令。除非被 --noprofile选项禁止了; 在非登录shell里,环境信息只读取 /etc/bash.bashrc和~/.bashrc 手工执行是属于登陆shell,脚本执行数据非登陆shell,而我的linux环境配置中只对/etc/profile进行了jdk1.7等环境的配置,所以脚本执行/usr/local/zookeeper/bin/zkServer.sh start 启动zookeeper失败了 解决方法(下面3个方法任选1): 1、脚本代码中添加“source /etc/profile;” 改为:ssh crxy$i "source /etc/profile;/usr/local/zookeeper/bin/zkServer.sh start" 2、把profile的配置信息echo到.bashrc中 echo 'source /etc/profile' >> ~/.bashrc 3、在/zookeeper/bin/zkEnv.sh的中开始位置添加 export JAVA_HOME=/usr/local/jdk1.7.0_45(就像hadoop中对hadoop-env.sh的配置一样) 解决了这个问题,就以方便的通过脚本实现hadoop集群的启动与关闭了