Last active
January 2, 2018 08:36
-
-
Save myleshk/01935aba8a3b78f44f68e4b6cc9350e0 to your computer and use it in GitHub Desktop.
Init Script for Apache Kafka as Ubuntu Service (Deprecated)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |
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
(Only tested on Ubuntu 16.04)
Add script
/etc/init.d/kafka
sudo chmod 755 /etc/init.d/kafka
Load script into service:
sudo update-rc.d kafka defaults <priority>
(priority is integer like95
, and is optional)