The shell script started by the kafka cluster fails

1. Problem description

When executing the kafka cluster startup script written, it is found that the kafka cluster server has not started, and no error log has been reported.
The script is as follows:

#! /bin/bash
case $1 in
"start"){
    
    
    for i in bigdata111 bigdata112 bigdata113
    do
        echo " --------启动 $i Kafka-------"
        ssh $i "/opt/module/kafka/bin/kafka-server-start.sh -daemon /opt/module/kafka/config/server.properties"
    done
};;
"stop"){
    
    
    for i in bigdata111 bigdata112 bigdata113
    do
        echo " --------停止 $i Kafka-------"
        ssh $i "/opt/module/kafka/bin/kafka-server-stop.sh stop"
    done
};;
esac

problem causes:

Log-in shell, using a user name such as xxx to log in, will automatically load /etc/profile

Non-login shell, using ssh such as ssh 192.168.1.121 to log in, will not automatically load /etc/profile, but will automatically load ~/.bashrc

Two, the solution

1. First test whether ssh [bigdata111 ip] "which java" responds, if it shows "no found", you need to configure

[root@bigdata111 bin]# ssh bigdata112 "which java"
which: no java in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)

2. Configure the ~/.bashrc file

[root@bigdata111 bin]# vim ~/.bashrc 

Add the following configuration inside

export JAVA_HOME=/opt/module/jdk1.8.0_212
export PATH=$PATH:$JAVA_HOME/bin

And distribute the .bashrc file to other nodes

[root@bigdata111 bin]#scp ~/.bashrc root@bigdata112:/root/.bashrc

[root@bigdata111 bin]#scp ~/.bashrc root@bigdata113:/root/.bashrc

3. Continue to test ssh [bigdata111 ip] "which java" and restart the script after success to see if the process exists

[root@bigdata111 bin]# ssh bigdata111 "which java"
/opt/module/jdk1.8.0_212/bin/java

insert image description here
At this point, the kafka cluster can be started normally through the shell script

Guess you like

Origin blog.csdn.net/weixin_43520450/article/details/129468345