Skip to content

Instantly share code, notes, and snippets.

@jonathanmv
Created October 22, 2018 20:43
Show Gist options
  • Select an option

  • Save jonathanmv/101ab1a520c66b385089a3720a5de3ed to your computer and use it in GitHub Desktop.

Select an option

Save jonathanmv/101ab1a520c66b385089a3720a5de3ed to your computer and use it in GitHub Desktop.
kafka as a service amazon linux
#!/bin/bash
#/etc/init.d/kafka
#chkconfig: - 99 10
DAEMON_PATH=/home/ec2-user/kafka_2.11-2.0.0/bin
DAEMON_NAME=kafka
# Check that networking is up.
#[ ${NETWORKING} = "no" ] && exit 0
PATH=$PATH:$DAEMON_PATH
# See how we were called.
case "$1" in
start)
# Start daemon.
pid=`ps ax | grep -i 'kafka.Kafka' | grep -v grep | awk '{print $1}'`
if [ -n "$pid" ]
then
echo "Kafka is already running"
else
echo "Starting $DAEMON_NAME"
$DAEMON_PATH/kafka-server-start.sh -daemon /home/ec2-user/kafka_2.11-2.0.0/config/server.properties
fi
;;
stop)
echo "Shutting down $DAEMON_NAME"
$DAEMON_PATH/kafka-server-stop.sh
;;
restart)
$0 stop
sleep 2
$0 start
;;
status)
pid=`ps ax | grep -i 'kafka.Kafka' | grep -v grep | awk '{print $1}'`
if [ -n "$pid" ]
then
echo "Kafka is Running as PID: $pid"
else
echo "Kafka is not Running"
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit 0
@jonathanmv

jonathanmv commented Oct 22, 2018 •

Copy link
Copy Markdown
Author

This script is based on one published in the Kafka Cluster Setup on Udemy. The script in there was made for Ubuntu but I needed it to run in Amazon Linux. Instead of using update-rc.d it uses chkconfig
Set up kafka as a service so that you can do sudo service kafka start

sudo nano /etc/init.d/kafka
# Paste the contents in this gist and save the file
sudo chmod +x /etc/init.d/kafka
sudo chown root:root /etc/init.d/kafka
sudo chkconfig --add kafka
sudo chkconfig kafka on
sudo service kafka start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment