livy-server 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env bash
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # Runs Livy server.
  19. usage="Usage: livy-server (start|stop)"
  20. export LIVY_HOME=$(cd $(dirname $0)/.. && pwd)
  21. LIVY_CONF_DIR=${LIVY_CONF_DIR:-"$LIVY_HOME/conf"}
  22. if [ -f "${LIVY_CONF_DIR}/livy-env.sh" ]; then
  23. # Promote all variable declarations to environment (exported) variables
  24. set -a
  25. . "${LIVY_CONF_DIR}/livy-env.sh"
  26. set +a
  27. fi
  28. # Find the java binary
  29. if [ -n "${JAVA_HOME}" ]; then
  30. RUNNER="${JAVA_HOME}/bin/java"
  31. elif [ `command -v java` ]; then
  32. RUNNER="java"
  33. else
  34. echo "JAVA_HOME is not set" >&2
  35. exit 1
  36. fi
  37. LIVY_IDENT_STRING=${LIVY_IDENT_STRING:-"$USER"}
  38. LIVY_PID_DIR=${LIVY_PID_DIR:-"/tmp"}
  39. LIVY_MAX_LOG_FILES=${LIVY_MAX_LOG_FILES:-5}
  40. pid="$LIVY_PID_DIR/livy-$LIVY_IDENT_STRING-server.pid"
  41. livy_rotate_log() {
  42. log=$1
  43. num=$LIVY_MAX_LOG_FILES
  44. if [ -f "$log" ]; then # rotate logs
  45. while [ $num -gt 1 ]; do
  46. prev=`expr $num - 1`
  47. [ -f "$log.$prev" ] && mv "$log.$prev" "$log.$num"
  48. num=$prev
  49. done
  50. mv "$log" "$log.$num"
  51. fi
  52. }
  53. create_dir() {
  54. dir_name=$1
  55. dir_variable=$2
  56. if [ ! -d "$dir_name" ]; then
  57. mkdir -p $dir_name
  58. fi
  59. if [ ! -w "$dir_name" ]; then
  60. echo "$USER doesn't have permission to write to directory $dir_name (defined by $dir_variable)."
  61. exit 1
  62. fi
  63. }
  64. start_livy_server() {
  65. LIBDIR="$LIVY_HOME/jars"
  66. if [ ! -d "$LIBDIR" ]; then
  67. LIBDIR="$LIVY_HOME/server/target/jars"
  68. fi
  69. if [ ! -d "$LIBDIR" ]; then
  70. echo "Could not find Livy jars directory." 1>&2
  71. exit 1
  72. fi
  73. LIVY_CLASSPATH="$LIBDIR/*:$LIVY_CONF_DIR"
  74. if [ -n "$SPARK_CONF_DIR" ]; then
  75. LIVY_CLASSPATH="$LIVY_CLASSPATH:$SPARK_CONF_DIR"
  76. fi
  77. if [ -n "$HADOOP_CONF_DIR" ]; then
  78. LIVY_CLASSPATH="$LIVY_CLASSPATH:$HADOOP_CONF_DIR"
  79. fi
  80. if [ -n "$YARN_CONF_DIR" ]; then
  81. LIVY_CLASSPATH="$LIVY_CLASSPATH:$YARN_CONF_DIR"
  82. fi
  83. command="$RUNNER $LIVY_SERVER_JAVA_OPTS -cp $LIVY_CLASSPATH:$CLASSPATH com.cloudera.livy.server.LivyServer"
  84. if [ $1 = "old" ]; then
  85. exec $command
  86. else
  87. # get log directory
  88. LIVY_LOG_DIR=${LIVY_LOG_DIR:-${LIVY_HOME}/logs}
  89. create_dir $LIVY_LOG_DIR "LIVY_LOG_DIR"
  90. create_dir $LIVY_PID_DIR "LIVY_PID_DIR"
  91. log="$LIVY_LOG_DIR/livy-$LIVY_IDENT_STRING-server.out"
  92. # Set default scheduling priority
  93. LIVY_NICENESS=${LIVY_NICENESS:-0}
  94. if [ -f "$pid" ]; then
  95. TARGET_ID="$(cat "$pid")"
  96. if [[ $(ps -p "$TARGET_ID" -o comm=) =~ "java" ]]; then
  97. echo "livy_server running as process $TARGET_ID. Stop it first."
  98. exit 1
  99. fi
  100. fi
  101. livy_rotate_log "$log"
  102. echo "starting $command, logging to $log"
  103. nohup nice -n "$LIVY_NICENESS" $command >> "$log" 2>&1 < /dev/null &
  104. newpid="$!"
  105. echo "$newpid" > "$pid"
  106. sleep 2
  107. # Check if the process has died; in that case we'll tail the log so the user can see
  108. if [[ ! $(ps -p "$newpid" -o comm=) =~ "java" ]]; then
  109. echo "failed to launch $command:"
  110. tail -2 "$log" | sed 's/^/ /'
  111. echo "full log in $log"
  112. rm -rf "$pid"
  113. fi
  114. fi
  115. }
  116. option=$1
  117. case $option in
  118. (start)
  119. start_livy_server "new"
  120. ;;
  121. ("")
  122. # make it compatible with previous version of livy-server
  123. start_livy_server "old"
  124. ;;
  125. (stop)
  126. if [ -f $pid ]; then
  127. TARGET_ID="$(cat "$pid")"
  128. if [[ $(ps -p "$TARGET_ID" -o comm=) =~ "java" ]]; then
  129. echo "stopping livy_server"
  130. kill "$TARGET_ID" && rm -f "$pid"
  131. else
  132. echo "no livy_server to stop"
  133. fi
  134. else
  135. echo "no livy_server to stop"
  136. fi
  137. ;;
  138. (*)
  139. echo $usage
  140. exit 1
  141. ;;
  142. esac