zkServer.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper.out"
  120. case $1 in
  121. start)
  122. echo -n "Starting zookeeper ... "
  123. if [ -f "$ZOOPIDFILE" ]; then
  124. if kill -0 `cat "$ZOOPIDFILE"` > /dev/null 2>&1; then
  125. echo $command already running as process `cat "$ZOOPIDFILE"`.
  126. exit 0
  127. fi
  128. fi
  129. nohup "$JAVA" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \
  130. "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  131. -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
  132. if [ $? -eq 0 ]
  133. then
  134. if /bin/echo -n $! > "$ZOOPIDFILE"
  135. then
  136. sleep 1
  137. echo STARTED
  138. else
  139. echo FAILED TO WRITE PID
  140. exit 1
  141. fi
  142. else
  143. echo SERVER DID NOT START
  144. exit 1
  145. fi
  146. ;;
  147. start-foreground)
  148. ZOO_CMD=(exec "$JAVA")
  149. if [ "${ZOO_NOEXEC}" != "" ]; then
  150. ZOO_CMD=("$JAVA")
  151. fi
  152. "${ZOO_CMD[@]}" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \
  153. "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  154. -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG"
  155. ;;
  156. print-cmd)
  157. 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"
  158. ;;
  159. stop)
  160. echo -n "Stopping zookeeper ... "
  161. if [ ! -f "$ZOOPIDFILE" ]
  162. then
  163. echo "no zookeeper to stop (could not find file $ZOOPIDFILE)"
  164. else
  165. $KILL $(cat "$ZOOPIDFILE")
  166. rm "$ZOOPIDFILE"
  167. echo STOPPED
  168. fi
  169. exit 0
  170. ;;
  171. restart)
  172. shift
  173. "$0" stop ${@}
  174. sleep 3
  175. "$0" start ${@}
  176. ;;
  177. status)
  178. # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output
  179. clientPortAddress=`grep "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'`
  180. if ! [ $clientPortAddress ]
  181. then
  182. clientPortAddress="localhost"
  183. fi
  184. clientPort=`grep "^[[:space:]]*clientPort[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'`
  185. if ! [[ "$clientPort" =~ ^[0-9]+$ ]]
  186. then
  187. dataDir=`grep "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//'`
  188. myid=`cat "$dataDir/myid"`
  189. if ! [[ "$myid" =~ ^[0-9]+$ ]] ; then
  190. echo "clientPort not found and myid could not be determined. Terminating."
  191. exit 1
  192. fi
  193. clientPortAndAddress=`grep "^[[:space:]]*server.$myid=.*;.*" "$ZOOCFG" | sed -e 's/.*=//' | sed -e 's/.*;//'`
  194. if [ ! "$clientPortAndAddress" ] ; then
  195. echo "Client port not found in static config file. Looking in dynamic config file."
  196. dynamicConfigFile=`grep "^[[:space:]]*dynamicConfigFile" "$ZOOCFG" | sed -e 's/.*=//'`
  197. clientPortAndAddress=`grep "^[[:space:]]*server.$myid=.*;.*" "$dynamicConfigFile" | sed -e 's/.*=//' | sed -e 's/.*;//'`
  198. fi
  199. if [ ! "$clientPortAndAddress" ] ; then
  200. echo "Client port not found. Terminating."
  201. exit 1
  202. fi
  203. if [[ "$clientPortAndAddress" =~ ^.*:[0-9]+ ]] ; then
  204. clientPortAddress=`echo "$clientPortAndAddress" | sed -e 's/:.*//'`
  205. fi
  206. clientPort=`echo "$clientPortAndAddress" | sed -e 's/.*://'`
  207. if [ ! "$clientPort" ] ; then
  208. echo "Client port not found. Terminating."
  209. exit 1
  210. fi
  211. fi
  212. echo "Client port found: $clientPort. Client address: $clientPortAddress."
  213. STAT=`"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  214. -cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.client.FourLetterWordMain \
  215. $clientPortAddress $clientPort srvr 2> /dev/null \
  216. | grep Mode`
  217. if [ "x$STAT" = "x" ]
  218. then
  219. echo "Error contacting service. It is probably not running."
  220. exit 1
  221. else
  222. echo $STAT
  223. exit 0
  224. fi
  225. ;;
  226. *)
  227. echo "Usage: $0 [--config <conf-dir>] {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2
  228. esac