Skip to content

Instantly share code, notes, and snippets.

@mkhq
Forked from iandow/kafka
Last active February 6, 2018 11:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mkhq/d8170c20cd19561c9926a5fcc5e5eb06 to your computer and use it in GitHub Desktop.
Save mkhq/d8170c20cd19561c9926a5fcc5e5eb06 to your computer and use it in GitHub Desktop.
Simple Kafka Ubuntu init.d Startup Script
#!/bin/bash
DAEMON_PATH=/opt/kafka/
PATH=$PATH:$DAEMON_PATH/bin
# See how we were called.
case "$1" in
start)
# Start daemon.
echo "Starting Zookeeper";
nohup $DAEMON_PATH/bin/zookeeper-server-start.sh -daemon /$DAEMON_PATH/config/zookeeper.properties 2> /dev/null && \
echo "Starting Kafka";
nohup $DAEMON_PATH/bin/kafka-server-start.sh -daemon /$DAEMON_PATH/config/server.properties 2> /dev/null
;;
stop)
# Stop daemons.
echo "Shutting down Zookeeper";
pid=`ps ax | grep -i 'org.apache.zookeeper.server' | grep -v grep | awk '{print $1}'`
if [ -n "$pid" ]
then
kill -9 $pid
else
echo "Zookeeper was not Running"
fi
echo "Shutting down Kafka";
pid=`ps ax | grep -i 'kafka.Kafka' | grep -v grep | awk '{print $1}'`
if [ -n "$pid" ]
then
kill -9 $pid
else
echo "Kafka was not Running"
fi
;;
restart)
$0 stop
sleep 2
$0 start
;;
status)
pid=`ps ax | grep -i 'org.apache.zookeeper.server' | grep -v grep | awk '{print $1}'`
if [ -n "$pid" ]
then
echo "Zookeeper is Running as PID: $pid"
else
echo "Zookeeper is not Running"
fi
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
@masifpak
Copy link

masifpak commented Feb 4, 2017

When I start the script zookeeper start properly but kafka do not start. When I start the script again then kafka started.
To fix this issue i add 10 sec delay which work fine but this is not proper solution. What kafka wait for in zookeeper to start? why not start along with zookeeper?

@manmedia
Copy link

This is because when zookeeper needs some time to initialise and do leader/follower election. For your case, it's just 1 cluster with 1 node. So increasing syncLimit and initLimit in zookeeper.properties should take care of the job. I would say use the following:

tickTime=1000
initLimit=5
syncLimit=10

That's a good enough delay limit.

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