skip to Main Content

I’m developing an app that uses Kafka and I need a little help in setting up an aws server where I want a script to run when I boot the machine. I’ve created a script here:

#!/bin/sh
sh /home/ec2-user/kafka_2.13-3.4.0/bin/zookeeper-server-start.sh -daemon /home/ec2-user/kafka_2.13-3.4.0/config/zookeeper.properties &
sh /home/ec2-user/kafka_2.13-3.4.0/bin/kafka-server-start.sh -daemon /home/ec2-user/kafka_2.13-3.4.0/config/server0.properties &
sh /home/ec2-user/kafka_2.13-3.4.0/bin/kafka-server-start.sh -daemon /home/ec2-user/kafka_2.13-3.4.0/config/server1.properties &

cd /home/ec2-user/reimagined-guide/server/auth
/usr/bin/npm run pm2
cd /home/ec2-user/reimagined-guide/server/task
/usr/bin/npm run pm2
cd /home/ec2-user/reimagined-guide/server/bidding
/usr/bin/npm run pm2
cd /home/ec2-user/reimagined-guide/server/file-server
/usr/bin/npm run pm2

sudo systemctl start nginx

Everything works except the lines 2-4, which are related to Kafka (and which are all shell scripts). I need some help to configure this properly so my zookeeper and Kafka servers start when the machine turns on. Running these commands in the terminal works, but it doesn’t work through the script.

What I’ve tried:
Made all the files in the Kafka folder executable

sh /home/ec2-user/kafka_2.13-3.4.0/bin/zookeeper-server-start.sh -daemon /home/ec2-user/kafka_2.13-3.4.0/config/zookeeper.properties
cd /home/ec2-user/kafka_2.13-3.4.0/
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
/bin/bash/ bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

None of these have worked though. Like I mentioned before, I can run these on the terminal and they work fine, but not in my startup shell script. All these three were either from ChatGPT or from this websites posts.

3

Answers


  1. Chosen as BEST ANSWER

    I don't really understand how this works but it does:

    sh $HOME/kafka_2.13-3.4.0/bin/zookeeper-server-start.sh "-daemon" "./$HOME/kafka_2.13-3.4.0/config/zookeeper.properties" &
    sh $HOME/kafka_2.13-3.4.0/bin/kafka-server-start.sh "-daemon" "./$HOME/kafka_2.13-3.4.0/config/server0.properties" &
    sh $HOME/kafka_2.13-3.4.0/bin/kafka-server-start.sh "-daemon" "./$HOME/kafka_2.13-3.4.0/config/server1.properties" &
    

    Passing the arguments in quotations did the trick


  2. Use the following script.

    #!/bin/sh
    
    # Start ZooKeeper and Kafka servers in the background
    sh /home/ec2-user/kafka_2.13-3.4.0/bin/zookeeper-server-start.sh -daemon /home/ec2-user/kafka_2.13-3.4.0/config/zookeeper.properties &&
    sh /home/ec2-user/kafka_2.13-3.4.0/bin/kafka-server-start.sh -daemon /home/ec2-user/kafka_2.13-3.4.0/config/server0.properties &&
    sh /home/ec2-user/kafka_2.13-3.4.0/bin/kafka-server-start.sh -daemon /home/ec2-user/kafka_2.13-3.4.0/config/server1.properties &&
    echo "ZooKeeper and Kafka servers started successfully"
    
    # Define an array of Node.js application directories
    apps=(
      "/home/ec2-user/reimagined-guide/server/auth"
      "/home/ec2-user/reimagined-guide/server/task"
      "/home/ec2-user/reimagined-guide/server/bidding"
      "/home/ec2-user/reimagined-guide/server/file-server"
    )
    
    # Loop through the application directories and start each application with PM2
    for app in "${apps[@]}"
    do
      cd "$app" && /usr/bin/npm run pm2
      if [ $? -ne 0 ]
      then
        echo "Failed to start $app with PM2"
        exit 1
      fi
    done
    
    # Start Nginx server
    sudo systemctl start nginx
    if [ $? -ne 0 ]
    then
      echo "Failed to start Nginx server"
      exit 1
    fi
    
    echo "All services started successfully"
    
    Login or Signup to reply.
  3. let’s consider that you use a Linux system with systemd services mananger.
    You can run "systemctl –no-pager" to make sure.

    1º) locate the service name of kafka and zookeeper:

    systemctl --no-pager | grep -Ei "kafka|zookeeper"
    

    2º) get the name of kafka service, and locate the service file of kafka:

    systemctl show -p FragmentPath <KAFKA-SERVICE-NAME>
    

    3º) get the "FragmentPath" value and do a copy with another name:

    cd /usr/lib/systemd/system/
    cp /usr/lib/systemd/system/<KAFKA-SERVICE> /usr/lib/systemd/system/YOUR-SERVICE-NAME.service
    

    4º) edit your new service. Clear all content, and now remember that correctly configure the After, Wants and Requires sessions, it will ensure that your script will run after this services and require that they are up:

    [Unit]
    Description=My powerful script
    After=<KAFKA-SERVICE-NAME> <ZOOKEEPER-SERVICE-NAME>
    Wants=<KAFKA-SERVICE-NAME> <ZOOKEEPER-SERVICE-NAME>
    Requires=<KAFKA-SERVICE-NAME> <ZOOKEEPER-SERVICE-NAME>

    [Service]
    Type=oneshot
    ExecStart=/usr/bin/YOUR-SCRIPT.sh -s -q –timeout=10
    RemainAfterExit=no

    [Install]
    WantedBy=multi-user.target

    OBS:
    after: start order
    wants: desire start order
    Requires: Need to start

    6º) now, put your script in /usr/bin/YOUR-SCRIPT.sh (for example), and change perms:

    chmod 740 /usr/bin/YOUR-SCRIPT.sh
    

    7º) enable your new service:

    systemctl enable YOUR-SERVICE-NAME.service
    

    You will see a message like:

    Created symlink from
    /etc/systemd/system/multi-user.target.wants/YOUR-SERVICE-NAME.service to
    /etc/systemd/system/YOUR-SERVICE-NAME.service

    8º) restart the system, if u can, and verify now.

    this answer does not follow the best practices or the best parameters, it is simple and functional.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search