zkServer.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 "ZooKeeper JMX enabled by default" >&2
  32. if [ "x$JMXPORT" = "x" ]
  33. then
  34. # for some reason these two options are necessary on jdk6 on Ubuntu
  35. # accord to the docs they are not necessary, but otw jconsole cannot
  36. # do a local attach
  37. ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY org.apache.zookeeper.server.quorum.QuorumPeerMain"
  38. else
  39. if [ "x$JMXAUTH" = "x" ]
  40. then
  41. JMXAUTH=false
  42. fi
  43. if [ "x$JMXSSL" = "x" ]
  44. then
  45. JMXSSL=false
  46. fi
  47. if [ "x$JMXLOG4J" = "x" ]
  48. then
  49. JMXLOG4J=true
  50. fi
  51. echo "ZooKeeper remote JMX Port set to $JMXPORT" >&2
  52. echo "ZooKeeper remote JMX authenticate set to $JMXAUTH" >&2
  53. echo "ZooKeeper remote JMX ssl set to $JMXSSL" >&2
  54. echo "ZooKeeper remote JMX log4j set to $JMXLOG4J" >&2
  55. 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"
  56. fi
  57. else
  58. echo "JMX disabled by user request" >&2
  59. ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain"
  60. fi
  61. # use POSTIX interface, symlink is followed automatically
  62. ZOOBIN="${BASH_SOURCE-$0}"
  63. ZOOBIN="$(dirname "${ZOOBIN}")"
  64. ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)"
  65. if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then
  66. . "$ZOOBINDIR"/../libexec/zkEnv.sh
  67. else
  68. . "$ZOOBINDIR"/zkEnv.sh
  69. fi
  70. if [ "x$SERVER_JVMFLAGS" != "x" ]
  71. then
  72. JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS"
  73. fi
  74. if [ "x$2" != "x" ]
  75. then
  76. ZOOCFG="$ZOOCFGDIR/$2"
  77. fi
  78. # if we give a more complicated path to the config, don't screw around in $ZOOCFGDIR
  79. if [ "x$(dirname "$ZOOCFG")" != "x$ZOOCFGDIR" ]
  80. then
  81. ZOOCFG="$2"
  82. fi
  83. if $cygwin
  84. then
  85. ZOOCFG=`cygpath -wp "$ZOOCFG"`
  86. # cygwin has a "kill" in the shell itself, gets confused
  87. KILL=/bin/kill
  88. else
  89. KILL=kill
  90. fi
  91. echo "Using config: $ZOOCFG" >&2
  92. ZOO_DATADIR="$(grep "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')"
  93. ZOO_DATALOGDIR="$(grep "^[[:space:]]*dataLogDir" "$ZOOCFG" | sed -e 's/.*=//')"
  94. # iff autocreate is turned off and the datadirs don't exist fail
  95. # immediately as we can't create the PID file, etc..., anyway.
  96. if [ -n "$ZOO_DATADIR_AUTOCREATE_DISABLE" ]; then
  97. if [ ! -d "$ZOO_DATADIR/version-2" ]; then
  98. echo "ZooKeeper data directory is missing at $ZOO_DATADIR fix the path or run initialize"
  99. exit 1
  100. fi
  101. if [ -n "$ZOO_DATALOGDIR" ] && [ ! -d "$ZOO_DATALOGDIR/version-2" ]; then
  102. echo "ZooKeeper txnlog directory is missing at $ZOO_DATALOGDIR fix the path or run initialize"
  103. exit 1
  104. fi
  105. ZOO_DATADIR_AUTOCREATE="-Dzookeeper.datadir.autocreate=false"
  106. fi
  107. if [ -z "$ZOOPIDFILE" ]; then
  108. if [ ! -d "$ZOO_DATADIR" ]; then
  109. mkdir -p "$ZOO_DATADIR"
  110. fi
  111. ZOOPIDFILE="$ZOO_DATADIR/zookeeper_server.pid"
  112. else
  113. # ensure it exists, otw stop will fail
  114. mkdir -p "$(dirname "$ZOOPIDFILE")"
  115. fi
  116. if [ ! -w "$ZOO_LOG_DIR" ] ; then
  117. mkdir -p "$ZOO_LOG_DIR"
  118. fi
  119. ZOO_LOG_FILE=zookeeper-$USER-server-$HOSTNAME.log
  120. _ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper-$USER-server-$HOSTNAME.out"
  121. case $1 in
  122. start)
  123. echo -n "Starting zookeeper ... "
  124. if [ -f "$ZOOPIDFILE" ]; then
  125. if kill -0 `cat "$ZOOPIDFILE"` > /dev/null 2>&1; then
  126. echo $command already running as process `cat "$ZOOPIDFILE"`.
  127. exit 0
  128. fi
  129. fi
  130. nohup "$JAVA" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \
  131. "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  132. -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
  133. if [ $? -eq 0 ]
  134. then
  135. if /bin/echo -n $! > "$ZOOPIDFILE"
  136. then
  137. sleep 1
  138. pid=$(cat "${ZOOPIDFILE}")
  139. if ps -p "${pid}" > /dev/null 2>&1; then
  140. echo STARTED
  141. else
  142. echo FAILED TO START
  143. exit 1
  144. fi
  145. else
  146. echo FAILED TO WRITE PID
  147. exit 1
  148. fi
  149. else
  150. echo SERVER DID NOT START
  151. exit 1
  152. fi
  153. ;;
  154. start-foreground)
  155. ZOO_CMD=(exec "$JAVA")
  156. if [ "${ZOO_NOEXEC}" != "" ]; then
  157. ZOO_CMD=("$JAVA")
  158. fi
  159. "${ZOO_CMD[@]}" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \
  160. "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  161. -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG"
  162. ;;
  163. print-cmd)
  164. echo "\"$JAVA\" $ZOO_DATADIR_AUTOCREATE -Dzookeeper.log.dir=\"${ZOO_LOG_DIR}\" -Dzookeeper.root.logger=\"${ZOO_LOG4J_PROP}\" -Dzookeeper.log.file=\"${ZOO_LOG_FILE}\" -cp \"$CLASSPATH\" $JVMFLAGS $ZOOMAIN \"$ZOOCFG\" > \"$_ZOO_DAEMON_OUT\" 2>&1 < /dev/null"
  165. ;;
  166. stop)
  167. echo -n "Stopping zookeeper ... "
  168. if [ ! -f "$ZOOPIDFILE" ]
  169. then
  170. echo "no zookeeper to stop (could not find file $ZOOPIDFILE)"
  171. else
  172. $KILL $(cat "$ZOOPIDFILE")
  173. rm "$ZOOPIDFILE"
  174. echo STOPPED
  175. fi
  176. exit 0
  177. ;;
  178. restart)
  179. shift
  180. "$0" stop ${@}
  181. sleep 3
  182. "$0" start ${@}
  183. ;;
  184. status)
  185. # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output
  186. clientPortAddress=`grep "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'`
  187. if ! [ $clientPortAddress ]
  188. then
  189. clientPortAddress="localhost"
  190. fi
  191. clientPort=`grep "^[[:space:]]*clientPort[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'`
  192. if ! [[ "$clientPort" =~ ^[0-9]+$ ]]
  193. then
  194. dataDir=`grep "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//'`
  195. myid=`cat "$dataDir/myid"`
  196. if ! [[ "$myid" =~ ^[0-9]+$ ]] ; then
  197. echo "clientPort not found and myid could not be determined. Terminating."
  198. exit 1
  199. fi
  200. clientPortAndAddress=`grep "^[[:space:]]*server.$myid=.*;.*" "$ZOOCFG" | sed -e 's/.*=//' | sed -e 's/.*;//'`
  201. if [ ! "$clientPortAndAddress" ] ; then
  202. echo "Client port not found in static config file. Looking in dynamic config file."
  203. dynamicConfigFile=`grep "^[[:space:]]*dynamicConfigFile" "$ZOOCFG" | sed -e 's/.*=//'`
  204. clientPortAndAddress=`grep "^[[:space:]]*server.$myid=.*;.*" "$dynamicConfigFile" | sed -e 's/.*=//' | sed -e 's/.*;//'`
  205. fi
  206. if [ ! "$clientPortAndAddress" ] ; then
  207. echo "Client port not found. Terminating."
  208. exit 1
  209. fi
  210. if [[ "$clientPortAndAddress" =~ ^.*:[0-9]+ ]] ; then
  211. clientPortAddress=`echo "$clientPortAndAddress" | sed -e 's/:.*//'`
  212. fi
  213. clientPort=`echo "$clientPortAndAddress" | sed -e 's/.*://'`
  214. if [ ! "$clientPort" ] ; then
  215. echo "Client port not found. Terminating."
  216. exit 1
  217. fi
  218. fi
  219. echo "Client port found: $clientPort. Client address: $clientPortAddress."
  220. STAT=`"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \
  221. -cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.client.FourLetterWordMain \
  222. $clientPortAddress $clientPort srvr 2> /dev/null \
  223. | grep Mode`
  224. if [ "x$STAT" = "x" ]
  225. then
  226. echo "Error contacting service. It is probably not running."
  227. exit 1
  228. else
  229. echo $STAT
  230. exit 0
  231. fi
  232. ;;
  233. *)
  234. echo "Usage: $0 [--config <conf-dir>] {start|start-foreground|stop|restart|status|print-cmd}" >&2
  235. esac