一、现象
ssh 登入到远程机器执行zkServer.sh start
失败,但是手动执行确是可以执行成功的
二、定位错位日志
- 查看日志发现
[hadoop@hadoop001 logs]$ cat zookeeper.log
nohup: failed to run command ‘java’: No such file or directory
三、分析错误
[hadoop@hadoop001 logs]$ ssh hadoop001 "which java"
which: no java in (/usr/local/bin:/usr/bin)
[hadoop@hadoop001 logs]$ ssh hadoop001 "echo ${JAVA_HOME}"
/usr/java/jdk1.8.0_45
[hadoop@hadoop001 logs]$ ssh hadoop001 "echo ${PATH}"
/home/hadoop/app/hadoop/bin:/home/hadoop/app/hadoop/sbin:/home/hadoop/app/zookeeper/bin:/usr/java/jdk1.8.0_45/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin
[hadoop@hadoop001 logs]$ which java
/usr/java/jdk1.8.0_45/bin/java
发现ssh hadoop001 "which java"
找不到java
但是ssh hadoop001 "echo ${JAVA_HOME}"
打印可以找到java
本地也是安装配置了java的
四、查找zkServer.sh
#zkServer.sh start|stop|status
case $1 in
start)
echo -n "Starting zookeeper ... "
if [ -f "$ZOOPIDFILE" ]; then
if kill -0 `cat "$ZOOPIDFILE"` > /dev/null 2>&1; then
echo $command already running as process `cat "$ZOOPIDFILE"`.
exit 0
fi
fi
#后台开启zookeeper服务
nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
但是全局搜索找不到$JAVA
,应该是从其他脚本引入过来的,经过定位是从zkEnv.sh
过来的
# . 代表执行zkEnv.sh
if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then
. "$ZOOBINDIR/../libexec/zkEnv.sh"
else
. "$ZOOBINDIR/zkEnv.sh"
fi
五、查找zkEnv.sh
if [ "$JAVA_HOME" != "" ]; then
JAVA="$JAVA_HOME/bin/java"
else
JAVA=java
fi
找到$JAVA
位置,这时候我们可以测试下$JAVA_HOME
是否为空
发现$JAVA_HOME
为空
六、解决方案
- 写死路径
if [ "$JAVA_HOME" != "" ]; then JAVA="$JAVA_HOME/bin/java" else JAVA=/usr/java/jdk1.8.0_45/bin/java fi
- vi ~/.bashrc 添加java环境变量
ssh执行远程命令和脚本
bash模式: 加载环境变量配置文件: 个人.bashrc
所以当我们ssh时,加载的是远程用户的.bashrc文件[hadoop@hadoop001 ~]$ vi .bashrc export JAVA_HOME=/usr/java/jdk1.8.0_45 export PATH=$JAVA_HOME/bin:$PATH [hadoop@hadoop001 ~]$ source .bashrc
网友评论