zkServer.sh 9.1 KB

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