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
I don't really understand how this works but it does:
Passing the arguments in quotations did the trick
Use the following script.
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:
2º) get the name of kafka service, and locate the service file of kafka:
3º) get the "FragmentPath" value and do a copy with another name:
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:
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:
7º) enable your new service:
You will see a message like:
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.