livy-server 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. # Runs Livy server.
  18. usage="Usage: livy-server (start|stop|status)"
  19. export LIVY_HOME=$(cd $(dirname $0)/.. && pwd)
  20. LIVY_CONF_DIR=${LIVY_CONF_DIR:-"$LIVY_HOME/conf"}
  21. if [ -f "${LIVY_CONF_DIR}/livy-env.sh" ]; then
  22. # Promote all variable declarations to environment (exported) variables
  23. set -a
  24. . "${LIVY_CONF_DIR}/livy-env.sh"
  25. set +a
  26. fi
  27. # Find the java binary
  28. if [ -n "${JAVA_HOME}" ]; then
  29. RUNNER="${JAVA_HOME}/bin/java"
  30. elif [ `command -v java` ]; then
  31. RUNNER="java"
  32. else
  33. echo "JAVA_HOME is not set" >&2
  34. exit 1
  35. fi
  36. LIVY_IDENT_STRING=${LIVY_IDENT_STRING:-"$USER"}
  37. LIVY_PID_DIR=${LIVY_PID_DIR:-"/tmp"}
  38. LIVY_MAX_LOG_FILES=${LIVY_MAX_LOG_FILES:-5}
  39. pid="$LIVY_PID_DIR/livy-$LIVY_IDENT_STRING-server.pid"
  40. livy_rotate_log() {
  41. log=$1
  42. num=$LIVY_MAX_LOG_FILES
  43. if [ $LIVY_MAX_LOG_FILES -lt 1 ]; then
  44. num=5
  45. fi
  46. if [ -f "$log" ]; then # rotate logs
  47. while [ $num -gt 1 ]; do
  48. prev=`expr $num - 1`
  49. [ -f "$log.$prev" ] && mv "$log.$prev" "$log.$num"
  50. num=$prev
  51. done
  52. mv "$log" "$log.$num"
  53. fi
  54. }
  55. create_dir() {
  56. dir_name=$1
  57. dir_variable=$2
  58. if [ ! -d "$dir_name" ]; then
  59. mkdir -p $dir_name
  60. fi
  61. if [ ! -w "$dir_name" ]; then
  62. echo "$USER doesn't have permission to write to directory $dir_name (defined by $dir_variable)."
  63. exit 1
  64. fi
  65. }
  66. start_livy_server() {
  67. LIBDIR="$LIVY_HOME/jars"
  68. if [ ! -d "$LIBDIR" ]; then
  69. LIBDIR="$LIVY_HOME/server/target/jars"
  70. THRIFT_LIBDIR="$LIVY_HOME/thriftserver/server/target/jars"
  71. fi
  72. if [ ! -d "$LIBDIR" ]; then
  73. echo "Could not find Livy jars directory." 1>&2
  74. exit 1
  75. else
  76. if [ -d "$THRIFT_LIBDIR" ]; then
  77. LIBDIR="$THRIFT_LIBDIR/*:$LIBDIR"
  78. fi
  79. fi
  80. LIVY_CLASSPATH="${LIVY_CLASSPATH:-${LIBDIR}/*:${LIVY_CONF_DIR}}"
  81. if [ -n "$SPARK_CONF_DIR" ]; then
  82. LIVY_CLASSPATH="$LIVY_CLASSPATH:$SPARK_CONF_DIR"
  83. fi
  84. if [ -n "$HADOOP_CONF_DIR" ]; then
  85. LIVY_CLASSPATH="$LIVY_CLASSPATH:$HADOOP_CONF_DIR"
  86. fi
  87. if [ -n "$YARN_CONF_DIR" ]; then
  88. LIVY_CLASSPATH="$LIVY_CLASSPATH:$YARN_CONF_DIR"
  89. fi
  90. command="$RUNNER $LIVY_SERVER_JAVA_OPTS -cp $LIVY_CLASSPATH:$CLASSPATH org.apache.livy.server.LivyServer"
  91. if [ $1 = "old" ]; then
  92. exec $command
  93. else
  94. # get log directory
  95. LIVY_LOG_DIR=${LIVY_LOG_DIR:-${LIVY_HOME}/logs}
  96. create_dir $LIVY_LOG_DIR "LIVY_LOG_DIR"
  97. create_dir $LIVY_PID_DIR "LIVY_PID_DIR"
  98. log="$LIVY_LOG_DIR/livy-$LIVY_IDENT_STRING-server.out"
  99. # Set default scheduling priority
  100. LIVY_NICENESS=${LIVY_NICENESS:-0}
  101. if [ -f "$pid" ]; then
  102. TARGET_ID="$(cat "$pid")"
  103. if [[ $(ps -p "$TARGET_ID" -o comm=) =~ "java" ]]; then
  104. echo "livy-server running as process $TARGET_ID. Stop it first."
  105. exit 1
  106. fi
  107. fi
  108. livy_rotate_log "$log"
  109. echo "starting $command, logging to $log"
  110. nohup nice -n "$LIVY_NICENESS" $command >> "$log" 2>&1 < /dev/null &
  111. newpid="$!"
  112. echo "$newpid" > "$pid"
  113. sleep 2
  114. # Check if the process has died; in that case we'll tail the log so the user can see
  115. if [[ ! $(ps -p "$newpid" -o comm=) =~ "java" ]]; then
  116. echo "failed to launch $command:"
  117. tail -2 "$log" | sed 's/^/ /'
  118. echo "full log in $log"
  119. rm -rf "$pid"
  120. exit 1
  121. fi
  122. fi
  123. }
  124. option=$1
  125. case $option in
  126. (start)
  127. start_livy_server "new"
  128. ;;
  129. ("")
  130. # make it compatible with previous version of livy-server
  131. start_livy_server "old"
  132. ;;
  133. (stop)
  134. if [ -f "$pid" ]; then
  135. TARGET_ID="$(cat "$pid")"
  136. if [[ $(ps -p "$TARGET_ID" -o comm=) =~ "java" ]]; then
  137. echo "stopping livy-server"
  138. kill "$TARGET_ID" && rm -f "$pid"
  139. else
  140. echo "no livy-server to stop"
  141. fi
  142. else
  143. echo "no livy-server to stop"
  144. fi
  145. ;;
  146. (status)
  147. if [ -f "$pid" ]; then
  148. TARGET_ID="$(cat "$pid")"
  149. if [[ $(ps -p "$TARGET_ID" -o comm=) =~ "java" ]]; then
  150. echo "livy-server is running (pid: $TARGET_ID)"
  151. else
  152. echo "livy-server is not running"
  153. fi
  154. else
  155. echo "livy-server is not running"
  156. fi
  157. ;;
  158. (*)
  159. echo $usage
  160. exit 1
  161. ;;
  162. esac