zkServer.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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" ]
  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. if [ -z $ZOOPIDFILE ]; then
  72. ZOO_DATADIR=$(grep "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')
  73. if [ ! -d "$ZOO_DATADIR" ]; then
  74. mkdir -p "$ZOO_DATADIR"
  75. fi
  76. ZOOPIDFILE="$ZOO_DATADIR/zookeeper_server.pid"
  77. else
  78. # ensure it exists, otw stop will fail
  79. mkdir -p $(dirname "$ZOOPIDFILE")
  80. fi
  81. _ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper.out"
  82. case $1 in
  83. start)
  84. echo -n "Starting zookeeper ... "
  85. if [ -f $ZOOPIDFILE ]; then
  86. if kill -0 `cat $ZOOPIDFILE` > /dev/null 2>&1; then
  87. echo $command already running as process `cat $ZOOPIDFILE`.
  88. exit 0
  89. fi
  90. fi
  91. nohup $JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  92. -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
  93. if [ $? -eq 0 ]
  94. then
  95. if /bin/echo -n $! > "$ZOOPIDFILE"
  96. then
  97. sleep 1
  98. echo STARTED
  99. else
  100. echo FAILED TO WRITE PID
  101. exit 1
  102. fi
  103. else
  104. echo SERVER DID NOT START
  105. exit 1
  106. fi
  107. ;;
  108. start-foreground)
  109. $JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  110. -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG"
  111. ;;
  112. print-cmd)
  113. echo "$JAVA -Dzookeeper.log.dir=\"${ZOO_LOG_DIR}\" -Dzookeeper.root.logger=\"${ZOO_LOG4J_PROP}\" -cp \"$CLASSPATH\" $JVMFLAGS $ZOOMAIN \"$ZOOCFG\" > \"$_ZOO_DAEMON_OUT\" 2>&1 < /dev/null"
  114. ;;
  115. stop)
  116. echo -n "Stopping zookeeper ... "
  117. if [ ! -f "$ZOOPIDFILE" ]
  118. then
  119. echo "error: could not find file $ZOOPIDFILE"
  120. exit 1
  121. else
  122. $KILL -9 $(cat "$ZOOPIDFILE")
  123. rm "$ZOOPIDFILE"
  124. echo STOPPED
  125. exit 0
  126. fi
  127. ;;
  128. upgrade)
  129. shift
  130. echo "upgrading the servers to 3.*"
  131. $JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  132. -cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.server.upgrade.UpgradeMain ${@}
  133. echo "Upgrading ... "
  134. ;;
  135. restart)
  136. shift
  137. "$0" stop ${@}
  138. sleep 3
  139. "$0" start ${@}
  140. ;;
  141. status)
  142. # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output
  143. STAT=`echo stat | nc -q 1 localhost $(grep "^[[:space:]]*clientPort" "$ZOOCFG" | sed -e 's/.*=//') 2> /dev/null| grep Mode`
  144. if [ "x$STAT" = "x" ]
  145. then
  146. echo "Error contacting service. It is probably not running."
  147. exit 1
  148. else
  149. echo $STAT
  150. exit 0
  151. fi
  152. ;;
  153. *)
  154. echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2
  155. esac