1

i have seen that the developers of zookeeper have a great zkServer.sh script to start,stop, restart etc zookeeper, but i have no idea why in the world they decided to skip init script to use.

With the zkServer.sh script no way to set zookeeper to run on startup of the server for example.

So anyone know how to wrap around or a usable and correctly working zookeeper init script?

here is link to the zkServer.sh file https://github.com/apache/zookeeper/blob/master/bin/zkServer.sh

Thanks for your help

Here is my example init script that i just need some help tunning

#!/bin/bash
#
# /etc/init.d/zookeeper
#
# Startup script for Zookeeper
#
# chkconfig: 2345 80 20
# description: Starts and stops Zookeeper
# pidfile: /var/run/zookeeper/zookeeper.pid

### BEGIN INIT INFO
# Provides:          zookeeper
# Required-Start:    $remote_fs $network $named $time
# Required-Stop:     $remote_fs $network $named $time
# Should-Start:      ntp mdadm
# Should-Stop:       ntp mdadm
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: distributed storage system for structured data
# Description:       Zookeeper is a distributed (peer-to-peer) system for
#                    the management and storage of structured data.
### END INIT INFO

. /etc/rc.d/init.d/functions

#export ZK_HOME=/usr/share/zookeeper
#export ZK_CONF=/etc/zookeeper/conf
#export ZK_INCLUDE=$ZK_HOME/zookeeper.in.sh
export ZK_HOME=/zookeeper/opt/zookeeper
export ZK_CONF=/zookeeper/opt/zookeeper/conf
export ZK_INCLUDE=$ZK_HOME/bin/zookeeper.in.sh

#export ZK_OWNR=zookeeper
export ZK_OWNR=root
NAME="zookeeper"
log_file=/var/log/zookeeper/zookeeper.log
pid_file=/var/run/zookeeper/zookeeper.pid
lock_file=/var/lock/subsys/$NAME
#ZK_PROG=/usr/sbin/zookeeper
ZK_PROG=/zookeeper/opt/zookeeper/bin/zkServer.sh

# The first existing directory is used for JAVA_HOME if needed.
JVM_SEARCH_DIRS="/usr/lib/jvm/jre /usr/lib/jvm/jre-1.7.* /usr/lib/jvm/java-1.7.*/jre"

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# If JAVA_HOME has not been set, try to determine it.
if [ -z "$JAVA_HOME" ]; then
    # If java is in PATH, use a JAVA_HOME that corresponds to that. This is
    # both consistent with how the upstream startup script works, and with
    # the use of alternatives to set a system JVM (as is done on Debian and
    # Red Hat derivatives).
    java="`/usr/bin/which java 2>/dev/null`"
    if [ -n "$java" ]; then
        java=`readlink --canonicalize "$java"`
        JAVA_HOME=`dirname "\`dirname \$java\`"`
    else
        # No JAVA_HOME set and no java found in PATH; search for a JVM.
        for jdir in $JVM_SEARCH_DIRS; do
            if [ -x "$jdir/bin/java" ]; then
                JAVA_HOME="$jdir"
                break
            fi
        done
        # if JAVA_HOME is still empty here, punt.
    fi
fi
JAVA="$JAVA_HOME/bin/java"
export JAVA_HOME JAVA

case "$1" in
    start)
        # Zookeeper startup
        echo -n "Starting Zookeeper: "
        [ -d `dirname "$pid_file"` ] || \
            install -m 755 -o $ZK_OWNR -g $ZK_OWNR -d `dirname $pid_file`
        $ZK_PROG start /zookeeper/opt/zookeeper/conf/zoo.cfg $ZK_CONF -p $pid_file > $log_file 2>&1
        retval=$?
        [ $retval -eq 0 ] && touch $lock_file
        echo "OK"
        ;;
    stop)
        # Zookeeper shutdown
        echo -n "Shutdown Zookeeper: "
        kill `cat $pid_file`
        retval=$?
        [ $retval -eq 0 ] && rm -f $lock_file
        for t in `seq 40`; do
            status -p $pid_file zookeeper > /dev/null 2>&1
            retval=$?
            if [ $retval -eq 3 ]; then
                echo "OK"
                exit 0
            else
                sleep 0.5
            fi;
        done
        # Adding a sleep here to give jmx time to wind down (ZK-4483). Not ideal...
        # Adam Holmberg suggests this, but that would break if the jmx port is changed
        # for t in `seq 40`; do netstat -tnlp | grep "0.0.0.0:7199" > /dev/null 2>&1 && sleep 0.1 || break; done
        sleep 5
        status -p $pid_file zookeeper > /dev/null 2>&1
        retval=$?
        if [ $retval -eq 3 ]; then
            echo "OK"
        else
            echo "ERROR: could not stop $NAME"
            exit 1
        fi
        ;;
    reload|restart)
        $0 stop
        $0 start
        ;;
    status)
        status -p $pid_file zookeeper
        exit $?
        ;;
    *)
        echo "Usage: `basename $0` start|stop|status|restart|reload"
        exit 1
esac

exit 0

The problem now is if i do service zookeerper start it never completes and if i go look at the running zookeeper services from another terminal session, i see like 3 of them..and when i do service zookeeper status i get the following

● zookeeper.service - LSB: distributed storage system for structured data
   Loaded: loaded (/etc/rc.d/init.d/zookeeper; bad; vendor preset: disabled)
   Active: failed (Result: timeout) since Thu 2017-08-24 18:11:00 UTC; 2min 3s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 19673 ExecStart=/etc/rc.d/init.d/zookeeper start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/zookeeper.service
           └─19691 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.141-2.b16.el7_4.x86_64/jre/bin/java -Dzookeeper.log.dir=. -Dzookeepe...

Aug 24 18:06:00 10.34.227.131 systemd[1]: Starting LSB: distributed storage system for structured data...
Aug 24 18:06:01 10.34.227.131 zookeeper[19673]: Starting Zookeeper: OK
Aug 24 18:06:01 10.34.227.131 systemd[1]: PID file /var/run/zookeeper/zookeeper.pid not readable (yet?) after start.
Aug 24 18:11:00 10.34.227.131 systemd[1]: zookeeper.service start operation timed out. Terminating.
Aug 24 18:11:00 10.34.227.131 systemd[1]: Failed to start LSB: distributed storage system for structured data.
Aug 24 18:11:00 10.34.227.131 systemd[1]: Unit zookeeper.service entered failed state.
Aug 24 18:11:00 10.34.227.131 systemd[1]: zookeeper.service failed.
  • 1
    You're using systemd, so use a systemd service file. Regarding the failing start: Have a look into the Zookeeper logs. – gf_ Aug 24 '17 at 18:22
0

This what what worked

# ZooKeeper install path (where you extracted the tarball)
ZOOKEEPER='/zookeeper/opt/zookeeper'
ZOOUSER=root

source /etc/rc.d/init.d/functions
source $ZOOKEEPER/bin/zkEnv.sh

RETVAL=0
PIDFILE="/var/run/zookeeper/zookeeper.pid"
desc="ZooKeeper daemon"

start() {
  echo -n $"Starting $desc (zookeeper): "
  daemon --user $ZOOUSER $ZOOKEEPER/bin/zkServer.sh start
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/zookeeper
  return $RETVAL
}

stop() {
  echo -n $"Stopping $desc (zookeeper): "
  daemon --user $ZOOUSER $ZOOKEEPER/bin/zkServer.sh stop
  RETVAL=$?
  sleep 5
  echo
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/zookeeper $PIDFILE
}

restart() {
  stop
  start
}

get_pid() {
  cat "$PIDFILE"
}

checkstatus(){
  status -p $PIDFILE ${JAVA_HOME}/bin/java
  RETVAL=$?
}

condrestart(){
  [ -e /var/lock/subsys/zookeeper ] && restart || :
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    checkstatus
    ;;
  restart)
    restart
    ;;
  condrestart)
    condrestart
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart}"
    exit 1
esac

exit $RETVAL
0

You may need to install lsb-core (apt-get install lsb-core) in order to utilise the functions included at "source /etc/rc.d/init.d/functions". The path will be different. For myself (Ubuntu Server 16.04.5 LTS), the path was "/lib/lsb/init-functions" not "/etc/rc.d/init.d/functions"

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.