ctubbsii commented on code in PR #2224: URL: https://github.com/apache/zookeeper/pull/2224#discussion_r1970558522
########## bin/zkServer.sh: ########## @@ -1,320 +1,312 @@ -#!/usr/bin/env bash - -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at +#! /usr/bin/env bash +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 # -# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. # -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. # # If this scripted is run out of /usr/bin or some other system bin directory # it should be linked to and not copied. Things like java jar files are found # relative to the canonical path of this script. # - # use POSIX interface, symlink is followed automatically ZOOBIN="${BASH_SOURCE-$0}" -ZOOBIN="$(dirname "${ZOOBIN}")" -ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)" +ZOOBIN="$(dirname "$ZOOBIN")" +ZOOBINDIR="$(cd "$ZOOBIN" && pwd)" -if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then +if [[ -e "$ZOOBIN/../libexec/zkEnv.sh" ]]; then + # shellcheck source=bin/zkEnv.sh . "$ZOOBINDIR"/../libexec/zkEnv.sh else + # shellcheck source=bin/zkEnv.sh . "$ZOOBINDIR"/zkEnv.sh fi # See the following page for extensive details on setting # up the JVM to accept JMX remote management: # http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html # by default we allow local JMX connections -if [ "x$JMXLOCALONLY" = "x" ] -then - JMXLOCALONLY=false +if [[ -z $JMXLOCALONLY ]]; then + JMXLOCALONLY=false fi -if [ "x$JMXDISABLE" = "x" ] || [ "$JMXDISABLE" = 'false' ] -then +if [[ -z $JMXDISABLE ]] || [[ $JMXDISABLE == 'false' ]]; then echo "ZooKeeper JMX enabled by default" >&2 - if [ "x$JMXPORT" = "x" ] - then + if [[ -z $JMXPORT ]]; then # for some reason these two options are necessary on jdk6 on Ubuntu # accord to the docs they are not necessary, but otw jconsole cannot # do a local attach - ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY org.apache.zookeeper.server.quorum.QuorumPeerMain" + ZOOMAIN=("-Dcom.sun.management.jmxremote" "-Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY" "org.apache.zookeeper.server.quorum.QuorumPeerMain") else - if [ "x$JMXAUTH" = "x" ] - then + if [[ -z $JMXAUTH ]]; then JMXAUTH=false fi - if [ "x$JMXSSL" = "x" ] - then + if [[ -z $JMXSSL ]]; then JMXSSL=false fi - if [ "x$JMXLOG4J" = "x" ] - then + if [[ -z $JMXLOG4J ]]; then JMXLOG4J=true fi echo "ZooKeeper remote JMX Port set to $JMXPORT" >&2 echo "ZooKeeper remote JMX authenticate set to $JMXAUTH" >&2 echo "ZooKeeper remote JMX ssl set to $JMXSSL" >&2 echo "ZooKeeper remote JMX log4j set to $JMXLOG4J" >&2 - if [ "x$JMXHOSTNAME" = "x" ] - then - 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" + if [[ -z $JMXHOSTNAME ]]; then + 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") else echo "ZooKeeper remote JMX Hostname set to $JMXHOSTNAME" >&2 - ZOOMAIN="-Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=$JMXHOSTNAME -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" + ZOOMAIN=("-Dcom.sun.management.jmxremote" "-Djava.rmi.server.hostname=$JMXHOSTNAME" "-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") fi fi else - echo "JMX disabled by user request" >&2 - ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain" + echo "JMX disabled by user request" >&2 + ZOOMAIN=("org.apache.zookeeper.server.quorum.QuorumPeerMain") fi -if [ "x$SERVER_JVMFLAGS" != "x" ] -then - JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS" +if [[ -n $SERVER_JVMFLAGS ]]; then + JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS" fi +# shellcheck disable=SC2206 +flags=($JVMFLAGS) +# shellcheck disable=SC2206 +clientflags=($CLIENT_JVMFLAGS) -if [ "x$2" != "x" ] -then - ZOOCFG="$ZOOCFGDIR/$2" +if [[ -n $2 ]]; then + ZOOCFG="$ZOOCFGDIR/$2" fi # if we give a more complicated path to the config, don't screw around in $ZOOCFGDIR -if [ "x$(dirname "$ZOOCFG")" != "x$ZOOCFGDIR" ] -then - ZOOCFG="$2" +if [[ "$(dirname "$ZOOCFG")" != "$ZOOCFGDIR" ]]; then + ZOOCFG="$2" fi -if $cygwin -then - ZOOCFG=`cygpath -wp "$ZOOCFG"` - # cygwin has a "kill" in the shell itself, gets confused - KILL=/bin/kill +if $cygwin; then + ZOOCFG=$(cygpath -wp "$ZOOCFG") + # cygwin has a "kill" in the shell itself, gets confused + KILL='/bin/kill' else - KILL=kill + KILL='kill' fi echo "Using config: $ZOOCFG" >&2 case "$OSTYPE" in -*solaris*) - GREP=/usr/xpg4/bin/grep - ;; -*) - GREP=grep - ;; + *solaris*) + GREP='/usr/xpg4/bin/grep' + ;; + *) + GREP='grep' + ;; esac ZOO_DATADIR="$($GREP "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')" -ZOO_DATADIR="$(echo -e "${ZOO_DATADIR}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" +ZOO_DATADIR="$(echo -e "$ZOO_DATADIR" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" ZOO_DATALOGDIR="$($GREP "^[[:space:]]*dataLogDir" "$ZOOCFG" | sed -e 's/.*=//')" # iff autocreate is turned off and the datadirs don't exist fail # immediately as we can't create the PID file, etc..., anyway. -if [ -n "$ZOO_DATADIR_AUTOCREATE_DISABLE" ]; then - if [ ! -d "$ZOO_DATADIR/version-2" ]; then - echo "ZooKeeper data directory is missing at $ZOO_DATADIR fix the path or run initialize" - exit 1 - fi +if [[ -n $ZOO_DATADIR_AUTOCREATE_DISABLE ]]; then + if [[ ! -d "$ZOO_DATADIR/version-2" ]]; then + echo "ZooKeeper data directory is missing at $ZOO_DATADIR fix the path or run initialize" + exit 1 + fi - if [ -n "$ZOO_DATALOGDIR" ] && [ ! -d "$ZOO_DATALOGDIR/version-2" ]; then - echo "ZooKeeper txnlog directory is missing at $ZOO_DATALOGDIR fix the path or run initialize" - exit 1 - fi - ZOO_DATADIR_AUTOCREATE="-Dzookeeper.datadir.autocreate=false" + if [[ -n $ZOO_DATALOGDIR ]] && [[ ! -d "$ZOO_DATALOGDIR/version-2" ]]; then + echo "ZooKeeper txnlog directory is missing at $ZOO_DATALOGDIR fix the path or run initialize" + exit 1 + fi + flags=("-Dzookeeper.datadir.autocreate=false" "${flags[@]}") fi -if [ -z "$ZOOPIDFILE" ]; then - if [ ! -d "$ZOO_DATADIR" ]; then - mkdir -p "$ZOO_DATADIR" - fi - ZOOPIDFILE="$ZOO_DATADIR/zookeeper_server.pid" +if [[ -z $ZOOPIDFILE ]]; then + if [[ ! -d $ZOO_DATADIR ]]; then + mkdir -p "$ZOO_DATADIR" + fi + ZOOPIDFILE="$ZOO_DATADIR/zookeeper_server.pid" else - # ensure it exists, otw stop will fail - mkdir -p "$(dirname "$ZOOPIDFILE")" + # ensure it exists, otw stop will fail + mkdir -p "$(dirname "$ZOOPIDFILE")" fi -if [ ! -w "$ZOO_LOG_DIR" ] ; then -mkdir -p "$ZOO_LOG_DIR" +if [[ ! -w $ZOO_LOG_DIR ]]; then + mkdir -p "$ZOO_LOG_DIR" fi ZOO_LOG_FILE=${ZOO_LOG_FILE:-zookeeper-$USER-server-$HOSTNAME.log} _ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper-$USER-server-$HOSTNAME.out" case $1 in -start) - echo -n "Starting zookeeper ... " - if [ -f "$ZOOPIDFILE" ]; then - if kill -0 `cat "$ZOOPIDFILE"` > /dev/null 2>&1; then - echo $command already running as process `cat "$ZOOPIDFILE"`. - exit 1 + start) + echo -n "Starting zookeeper ... " + if [[ -f $ZOOPIDFILE ]]; then + if kill -0 "$(cat "$ZOOPIDFILE")" &>/dev/null; then + echo "already running as process $(cat "$ZOOPIDFILE")." + exit 1 fi fi - nohup "$JAVA" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \ - "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \ - -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \ - -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null & - if [ $? -eq 0 ] - then - case "$OSTYPE" in + nohup "$JAVA" "-Dzookeeper.log.dir=$ZOO_LOG_DIR" \ + "-Dzookeeper.log.file=$ZOO_LOG_FILE" \ + -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \ + "${flags[@]}" "${ZOOMAIN[@]}" "$ZOOCFG" &>"$_ZOO_DAEMON_OUT" </dev/null & + case "$OSTYPE" in *solaris*) - /bin/echo "${!}\\c" > "$ZOOPIDFILE" + /bin/echo "${!}\\c" >"$ZOOPIDFILE" ;; aix*) - /bin/echo "$!\c" > "$ZOOPIDFILE" + /bin/echo "$!\c" >"$ZOOPIDFILE" ;; *) - /bin/echo -n $! > "$ZOOPIDFILE" + echo -n $! >"$ZOOPIDFILE" ;; - esac - if [ $? -eq 0 ]; - then - sleep 1 - pid=$(cat "${ZOOPIDFILE}") - if ps -p "${pid}" > /dev/null 2>&1; then - echo STARTED - else - echo FAILED TO START - exit 1 - fi + esac + # this checks the exit code of the echo statements to verify the PID was written + # shellcheck disable=SC2320,SC2181 + if [[ $? -eq 0 ]]; then + sleep 1 + pid=$(cat "$ZOOPIDFILE") + if ps -p "$pid" &>/dev/null; then + echo STARTED else - echo FAILED TO WRITE PID + echo FAILED TO START exit 1 fi else - echo SERVER DID NOT START + echo FAILED TO WRITE PID exit 1 fi ;; -start-foreground) + start-foreground) ZOO_CMD=(exec "$JAVA") - if [ "${ZOO_NOEXEC}" != "" ]; then + if [[ -n $ZOO_NOEXEC ]]; then ZOO_CMD=("$JAVA") fi - "${ZOO_CMD[@]}" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \ - "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \ - -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \ - -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" + "${ZOO_CMD[@]}" "-Dzookeeper.log.dir=$ZOO_LOG_DIR" \ + "-Dzookeeper.log.file=$ZOO_LOG_FILE" \ + -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \ + "${flags[@]}" "${ZOOMAIN[@]}" "$ZOOCFG" ;; -print-cmd) - echo "\"$JAVA\" $ZOO_DATADIR_AUTOCREATE -Dzookeeper.log.dir=\"${ZOO_LOG_DIR}\" \ - -Dzookeeper.log.file=\"${ZOO_LOG_FILE}\" \ + print-classpath) + echo "CLASSPATH=$CLASSPATH" + ;; Review Comment: @tisonkun Here is the command added to print the classpath. (I previously commented that I might have left it out of this PR, but I found it and deleted that earlier comment, which was incorrect.) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
