Skip to content

Instantly share code, notes, and snippets.

@myleshk
Last active January 2, 2018 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myleshk/01935aba8a3b78f44f68e4b6cc9350e0 to your computer and use it in GitHub Desktop.
Save myleshk/01935aba8a3b78f44f68e4b6cc9350e0 to your computer and use it in GitHub Desktop.
Init Script for Apache Kafka as Ubuntu Service (Deprecated)
#! /bin/sh
### BEGIN INIT INFO
# Provides: kafka
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Kafka server
### END INIT INFO
DAEMON_PATH=/path/to/kafka/ ### CHANGE THIS TO YOUR OWN PATH ###
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 -v grep | grep -i 'org\.apache\.zookeeper\.server' | 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 -v grep | grep -i 'kafka\.Kafka' | 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 -v grep | grep -i 'org\.apache\.zookeeper\.server' | 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 -v grep | grep -i 'kafka\.Kafka' | 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
@myleshk
Copy link
Author

myleshk commented Jan 2, 2018

(Only tested on Ubuntu 16.04)


  • Add script

    1. Add this script to /etc/init.d/kafka
    2. Run sudo chmod 755 /etc/init.d/kafka
  • Load script into service:

    1. sudo update-rc.d kafka defaults <priority> (priority is integer like 95, and is optional)

@myleshk
Copy link
Author

myleshk commented Jan 2, 2018

While this method still work under Ubuntu 16.04, it is kind of deprecated in latest Linux systems, and systemd is preferred.

Here is the solution using systemd on Ubuntu 16.04:
https://gist.github.com/myleshk/21e3edae82a73a70c72271e18bd5b108

P.S. This solution only runs Kafka as root user, and in the linked solution, user kafka is used.

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