zkServer.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/sh
  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. # Only follow symlinks if readlink supports it
  41. if readlink "$0" > /dev/null 2>&1
  42. then
  43. ZOOBIN=`readlink "$0"`
  44. else
  45. ZOOBIN="$0"
  46. fi
  47. ZOOBINDIR=`dirname "$ZOOBIN"`
  48. . "$ZOOBINDIR"/zkEnv.sh
  49. if [ "x$2" != "x" ]
  50. then
  51. ZOOCFG="$ZOOCFGDIR/$2"
  52. fi
  53. # if we give a more complicated path to the config, don't screw around in $ZOOCFGDIR
  54. if [ "x`dirname $ZOOCFG`" != "x$ZOOCFGDIR" ]
  55. then
  56. ZOOCFG="$2"
  57. fi
  58. if $cygwin
  59. then
  60. ZOOCFG=`cygpath -wp "$ZOOCFG"`
  61. # cygwin has a "kill" in the shell itself, gets confused
  62. KILL=/bin/kill
  63. else
  64. KILL=kill
  65. fi
  66. echo "Using config: $ZOOCFG" >&2
  67. if [ -z $ZOOPIDFILE ]; then
  68. ZOO_DATADIR=$(grep dataDir "$ZOOCFG" | sed -e 's/.*=//')
  69. if [ ! -d "$ZOO_DATADIR" ]; then
  70. mkdir -p "$ZOO_DATADIR"
  71. fi
  72. ZOOPIDFILE="$ZOO_DATADIR/zookeeper_server.pid"
  73. else
  74. # ensure it exists, otw stop will fail
  75. mkdir -p $(dirname "$ZOOPIDFILE")
  76. fi
  77. _ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper.out"
  78. case $1 in
  79. start)
  80. echo "Starting zookeeper ... "
  81. $JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  82. -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
  83. /bin/echo -n $! > "$ZOOPIDFILE"
  84. echo STARTED
  85. ;;
  86. start-foreground)
  87. $JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  88. -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG"
  89. ;;
  90. print-cmd)
  91. 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"
  92. ;;
  93. stop)
  94. echo "Stopping zookeeper ... "
  95. if [ ! -f "$ZOOPIDFILE" ]
  96. then
  97. echo "error: could not find file $ZOOPIDFILE"
  98. exit 1
  99. else
  100. $KILL -9 $(cat "$ZOOPIDFILE")
  101. rm "$ZOOPIDFILE"
  102. echo STOPPED
  103. fi
  104. ;;
  105. upgrade)
  106. shift
  107. echo "upgrading the servers to 3.*"
  108. $JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
  109. -cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.server.upgrade.UpgradeMain ${@}
  110. echo "Upgrading ... "
  111. ;;
  112. restart)
  113. shift
  114. "$0" stop ${@}
  115. sleep 3
  116. "$0" start ${@}
  117. ;;
  118. status)
  119. # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output
  120. STAT=`echo stat | nc -q 1 localhost $(grep clientPort "$ZOOCFG" | sed -e 's/.*=//') 2> /dev/null| grep Mode`
  121. if [ "x$STAT" = "x" ]
  122. then
  123. echo "Error contacting service. It is probably not running."
  124. else
  125. echo $STAT
  126. fi
  127. ;;
  128. *)
  129. echo "Usage: $0 {start|stop|restart|status}" >&2
  130. esac