"Fossies" - the Fresh Open Source Software Archive

Member "apache-zookeeper-3.5.6/bin/zkServer.sh" (8 Oct 2019, 9386 Bytes) of package /linux/misc/apache-zookeeper-3.5.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "zkServer.sh": 3.5.5_vs_3.5.6.

    1 #!/usr/bin/env bash
    2 
    3 # Licensed to the Apache Software Foundation (ASF) under one or more
    4 # contributor license agreements.  See the NOTICE file distributed with
    5 # this work for additional information regarding copyright ownership.
    6 # The ASF licenses this file to You under the Apache License, Version 2.0
    7 # (the "License"); you may not use this file except in compliance with
    8 # the License.  You may obtain a copy of the License at
    9 #
   10 #     http://www.apache.org/licenses/LICENSE-2.0
   11 #
   12 # Unless required by applicable law or agreed to in writing, software
   13 # distributed under the License is distributed on an "AS IS" BASIS,
   14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15 # See the License for the specific language governing permissions and
   16 # limitations under the License.
   17 
   18 #
   19 # If this scripted is run out of /usr/bin or some other system bin directory
   20 # it should be linked to and not copied. Things like java jar files are found
   21 # relative to the canonical path of this script.
   22 #
   23 
   24 
   25 # use POSTIX interface, symlink is followed automatically
   26 ZOOBIN="${BASH_SOURCE-$0}"
   27 ZOOBIN="$(dirname "${ZOOBIN}")"
   28 ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)"
   29 
   30 if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then
   31   . "$ZOOBINDIR"/../libexec/zkEnv.sh
   32 else
   33   . "$ZOOBINDIR"/zkEnv.sh
   34 fi
   35 
   36 # See the following page for extensive details on setting
   37 # up the JVM to accept JMX remote management:
   38 # http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html
   39 # by default we allow local JMX connections
   40 if [ "x$JMXLOCALONLY" = "x" ]
   41 then
   42     JMXLOCALONLY=false
   43 fi
   44 
   45 if [ "x$JMXDISABLE" = "x" ] || [ "$JMXDISABLE" = 'false' ]
   46 then
   47   echo "ZooKeeper JMX enabled by default" >&2
   48   if [ "x$JMXPORT" = "x" ]
   49   then
   50     # for some reason these two options are necessary on jdk6 on Ubuntu
   51     #   accord to the docs they are not necessary, but otw jconsole cannot
   52     #   do a local attach
   53     ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY org.apache.zookeeper.server.quorum.QuorumPeerMain"
   54   else
   55     if [ "x$JMXAUTH" = "x" ]
   56     then
   57       JMXAUTH=false
   58     fi
   59     if [ "x$JMXSSL" = "x" ]
   60     then
   61       JMXSSL=false
   62     fi
   63     if [ "x$JMXLOG4J" = "x" ]
   64     then
   65       JMXLOG4J=true
   66     fi
   67     echo "ZooKeeper remote JMX Port set to $JMXPORT" >&2
   68     echo "ZooKeeper remote JMX authenticate set to $JMXAUTH" >&2
   69     echo "ZooKeeper remote JMX ssl set to $JMXSSL" >&2
   70     echo "ZooKeeper remote JMX log4j set to $JMXLOG4J" >&2
   71     ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dzookeeper.jmx.log4j.disable=$JMXLOG4J org.apache.zookeeper.server.quorum.QuorumPeerMain"
   72   fi
   73 else
   74     echo "JMX disabled by user request" >&2
   75     ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain"
   76 fi
   77 
   78 if [ "x$SERVER_JVMFLAGS" != "x" ]
   79 then
   80     JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS"
   81 fi
   82 
   83 if [ "x$2" != "x" ]
   84 then
   85     ZOOCFG="$ZOOCFGDIR/$2"
   86 fi
   87 
   88 # if we give a more complicated path to the config, don't screw around in $ZOOCFGDIR
   89 if [ "x$(dirname "$ZOOCFG")" != "x$ZOOCFGDIR" ]
   90 then
   91     ZOOCFG="$2"
   92 fi
   93 
   94 if $cygwin
   95 then
   96     ZOOCFG=`cygpath -wp "$ZOOCFG"`
   97     # cygwin has a "kill" in the shell itself, gets confused
   98     KILL=/bin/kill
   99 else
  100     KILL=kill
  101 fi
  102 
  103 echo "Using config: $ZOOCFG" >&2
  104 
  105 case "$OSTYPE" in
  106 *solaris*)
  107   GREP=/usr/xpg4/bin/grep
  108   ;;
  109 *)
  110   GREP=grep
  111   ;;
  112 esac
  113 ZOO_DATADIR="$($GREP "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')"
  114 ZOO_DATADIR="$(echo -e "${ZOO_DATADIR}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  115 ZOO_DATALOGDIR="$($GREP "^[[:space:]]*dataLogDir" "$ZOOCFG" | sed -e 's/.*=//')"
  116 
  117 # iff autocreate is turned off and the datadirs don't exist fail
  118 # immediately as we can't create the PID file, etc..., anyway.
  119 if [ -n "$ZOO_DATADIR_AUTOCREATE_DISABLE" ]; then
  120     if [ ! -d "$ZOO_DATADIR/version-2" ]; then
  121         echo "ZooKeeper data directory is missing at $ZOO_DATADIR fix the path or run initialize"
  122         exit 1
  123     fi
  124 
  125     if [ -n "$ZOO_DATALOGDIR" ] && [ ! -d "$ZOO_DATALOGDIR/version-2" ]; then
  126         echo "ZooKeeper txnlog directory is missing at $ZOO_DATALOGDIR fix the path or run initialize"
  127         exit 1
  128     fi
  129     ZOO_DATADIR_AUTOCREATE="-Dzookeeper.datadir.autocreate=false"
  130 fi
  131 
  132 if [ -z "$ZOOPIDFILE" ]; then
  133     if [ ! -d "$ZOO_DATADIR" ]; then
  134         mkdir -p "$ZOO_DATADIR"
  135     fi
  136     ZOOPIDFILE="$ZOO_DATADIR/zookeeper_server.pid"
  137 else
  138     # ensure it exists, otw stop will fail
  139     mkdir -p "$(dirname "$ZOOPIDFILE")"
  140 fi
  141 
  142 if [ ! -w "$ZOO_LOG_DIR" ] ; then
  143 mkdir -p "$ZOO_LOG_DIR"
  144 fi
  145 
  146 ZOO_LOG_FILE=zookeeper-$USER-server-$HOSTNAME.log
  147 _ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper-$USER-server-$HOSTNAME.out"
  148 
  149 case $1 in
  150 start)
  151     echo  -n "Starting zookeeper ... "
  152     if [ -f "$ZOOPIDFILE" ]; then
  153       if kill -0 `cat "$ZOOPIDFILE"` > /dev/null 2>&1; then
  154          echo $command already running as process `cat "$ZOOPIDFILE"`.
  155          exit 1
  156       fi
  157     fi
  158     nohup "$JAVA" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \
  159     "-Dzookeeper.log.file=${ZOO_LOG_FILE}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  160     -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \
  161     -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
  162     if [ $? -eq 0 ]
  163     then
  164       case "$OSTYPE" in
  165       *solaris*)
  166         /bin/echo "${!}\\c" > "$ZOOPIDFILE"
  167         ;;
  168       *)
  169         /bin/echo -n $! > "$ZOOPIDFILE"
  170         ;;
  171       esac
  172       if [ $? -eq 0 ];
  173       then
  174         sleep 1
  175         pid=$(cat "${ZOOPIDFILE}")
  176         if ps -p "${pid}" > /dev/null 2>&1; then
  177           echo STARTED
  178         else
  179           echo FAILED TO START
  180           exit 1
  181         fi
  182       else
  183         echo FAILED TO WRITE PID
  184         exit 1
  185       fi
  186     else
  187       echo SERVER DID NOT START
  188       exit 1
  189     fi
  190     ;;
  191 start-foreground)
  192     ZOO_CMD=(exec "$JAVA")
  193     if [ "${ZOO_NOEXEC}" != "" ]; then
  194       ZOO_CMD=("$JAVA")
  195     fi
  196     "${ZOO_CMD[@]}" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \
  197     "-Dzookeeper.log.file=${ZOO_LOG_FILE}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  198     -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \
  199     -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG"
  200     ;;
  201 print-cmd)
  202     echo "\"$JAVA\" $ZOO_DATADIR_AUTOCREATE -Dzookeeper.log.dir=\"${ZOO_LOG_DIR}\" \
  203     -Dzookeeper.log.file=\"${ZOO_LOG_FILE}\" -Dzookeeper.root.logger=\"${ZOO_LOG4J_PROP}\" \
  204     -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \
  205     -cp \"$CLASSPATH\" $JVMFLAGS $ZOOMAIN \"$ZOOCFG\" > \"$_ZOO_DAEMON_OUT\" 2>&1 < /dev/null"
  206     ;;
  207 stop)
  208     echo -n "Stopping zookeeper ... "
  209     if [ ! -f "$ZOOPIDFILE" ]
  210     then
  211       echo "no zookeeper to stop (could not find file $ZOOPIDFILE)"
  212     else
  213       $KILL $(cat "$ZOOPIDFILE")
  214       rm "$ZOOPIDFILE"
  215       sleep 1
  216       echo STOPPED
  217     fi
  218     exit 0
  219     ;;
  220 restart)
  221     shift
  222     "$0" stop ${@}
  223     sleep 3
  224     "$0" start ${@}
  225     ;;
  226 status)
  227     # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output
  228     clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'`
  229     if ! [ $clientPortAddress ]
  230     then
  231     clientPortAddress="localhost"
  232     fi
  233     clientPort=`$GREP "^[[:space:]]*clientPort[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'`
  234     if ! [[ "$clientPort"  =~ ^[0-9]+$ ]]
  235     then
  236        dataDir=`$GREP "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//'`
  237        myid=`cat "$dataDir/myid"`
  238        if ! [[ "$myid" =~ ^[0-9]+$ ]] ; then
  239          echo "clientPort not found and myid could not be determined. Terminating."
  240          exit 1
  241        fi
  242        clientPortAndAddress=`$GREP "^[[:space:]]*server.$myid=.*;.*" "$ZOOCFG" | sed -e 's/.*=//' | sed -e 's/.*;//'`
  243        if [ ! "$clientPortAndAddress" ] ; then
  244            echo "Client port not found in static config file. Looking in dynamic config file."
  245            dynamicConfigFile=`$GREP "^[[:space:]]*dynamicConfigFile" "$ZOOCFG" | sed -e 's/.*=//'`
  246            clientPortAndAddress=`$GREP "^[[:space:]]*server.$myid=.*;.*" "$dynamicConfigFile" | sed -e 's/.*=//' | sed -e 's/.*;//'`
  247        fi
  248        if [ ! "$clientPortAndAddress" ] ; then
  249           echo "Client port not found. Terminating."
  250           exit 1
  251        fi
  252        if [[ "$clientPortAndAddress" =~ ^.*:[0-9]+ ]] ; then
  253           clientPortAddress=`echo "$clientPortAndAddress" | sed -e 's/:.*//'`
  254        fi
  255        clientPort=`echo "$clientPortAndAddress" | sed -e 's/.*://'`
  256        if [ ! "$clientPort" ] ; then
  257           echo "Client port not found. Terminating."
  258           exit 1
  259        fi
  260     fi
  261     echo "Client port found: $clientPort. Client address: $clientPortAddress."
  262     STAT=`"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \
  263              -cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.client.FourLetterWordMain \
  264              $clientPortAddress $clientPort srvr 2> /dev/null    \
  265           | $GREP Mode`
  266     if [ "x$STAT" = "x" ]
  267     then
  268         echo "Error contacting service. It is probably not running."
  269         exit 1
  270     else
  271         echo $STAT
  272         exit 0
  273     fi
  274     ;;
  275 *)
  276     echo "Usage: $0 [--config <conf-dir>] {start|start-foreground|stop|restart|status|print-cmd}" >&2
  277 
  278 esac