zkServer.sh 7.5 KB

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