-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathkafka
100 lines (87 loc) · 2.44 KB
/
kafka
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/sh
#
# This script was adapted from https://gist.github.com/bejean/b9ff72c6d2143e16e35d and
# https://gist.github.com/superscott/a1c67871cdd54b0c8693
#
# Other references:
# https://stackoverflow.com/questions/41014742/where-will-be-nohup-file-created-stored
#
# Purpose: This script starts and stops the Kafka daemon
#
# chkconfig: - 90 10
# description: Kafka daemon
### BEGIN INIT INFO
# Provides: kafka
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Controls Kafka as a Service
### END INIT INFO
INSTALL_DIR="/opt/kafka"
if [ ! -d "$INSTALL_DIR" ]; then
echo "$INSTALL_DIR not found! Please check the INSTALL_DIR setting in your $0 script."
exit 1
fi
# Specify the user to run Kafka as; if not set, then Kafka will not run.
RUNAS="kafka"
# verify the specified run as user exists
runas_uid="`id -u "$RUNAS"`"
if [ $? -ne 0 ]; then
echo "User $RUNAS not found! Please create the $RUNAS user before running this script."
exit 1
fi
DAEMON_NAME=kafka
MEMORY_LIMITS="-Xmx256M -Xms128M"
KAFKA_HEAP_OPTS="-Djava.security.auth.login.config=$INSTALL_DIR/config/jaas.conf -Djava.security.krb5.conf=/etc/krb5.conf $MEMORY_LIMITS"
get_pid () {
echo `ps ax | grep -i 'kafka.Kafka' | grep -v grep | grep -iv consumer | grep -iv producer | grep -i server-sasl-brokers-zookeeper.properties | awk '{print $1}'`
}
case "$1" in
start)
# Start daemon.
pid=$( get_pid )
if [ -n "$pid" ]
then
echo "Kafka was already running (pid: $pid)"
else
echo "Starting $DAEMON_NAME";
if [ -n "$RUNAS" ]; then
sudo su -c "export KAFKA_HEAP_OPTS=\"$KAFKA_HEAP_OPTS\" && nohup $INSTALL_DIR/bin/kafka-server-start.sh -daemon $INSTALL_DIR/config/server-sasl-brokers-zookeeper.properties > /tmp/nohup.out" - "$RUNAS"
else
echo "RUNAS user not specified"
fi
fi
;;
stop)
# Stop daemons.
echo "Shutting down $DAEMON_NAME";
pid=$( get_pid )
if [ -n "$pid" ]
then
kill -9 $pid
else
echo "Kafka was not Running"
fi
;;
restart)
$0 stop
sleep 2
$0 start
;;
status)
pid=$( get_pid )
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
esac
exit 0