dolphinscheduler-daemon.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # 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. usage="Usage: dolphinscheduler-daemon.sh (start|stop|status) <api-server|master-server|worker-server|alert-server|standalone-server> "
  19. # if no args specified, show usage
  20. if [ $# -le 1 ]; then
  21. echo $usage
  22. exit 1
  23. fi
  24. startStop=$1
  25. shift
  26. command=$1
  27. shift
  28. echo "Begin $startStop $command......"
  29. BIN_DIR=`dirname $0`
  30. BIN_DIR=`cd "$BIN_DIR"; pwd`
  31. DOLPHINSCHEDULER_HOME=`cd "$BIN_DIR/.."; pwd`
  32. BIN_ENV_FILE="${DOLPHINSCHEDULER_HOME}/bin/env/dolphinscheduler_env.sh"
  33. # Overwrite server dolphinscheduler_env.sh in path `<server>/conf/dolphinscheduler_env.sh` when exists
  34. # `bin/env/dolphinscheduler_env.sh` file. User could only change `bin/env/dolphinscheduler_env.sh` instead
  35. # of each server's dolphinscheduler_env.sh when they want to start the server
  36. function overwrite_server_env() {
  37. local server=$1
  38. local server_env_file="${DOLPHINSCHEDULER_HOME}/${server}/conf/dolphinscheduler_env.sh"
  39. if [ -f "${BIN_ENV_FILE}" ]; then
  40. echo "Overwrite ${server}/conf/dolphinscheduler_env.sh using bin/env/dolphinscheduler_env.sh."
  41. cp "${BIN_ENV_FILE}" "${server_env_file}"
  42. else
  43. echo "Start server ${server} using env config path ${server_env_file}, because file ${BIN_ENV_FILE} not exists."
  44. fi
  45. }
  46. export HOSTNAME=`hostname`
  47. export DOLPHINSCHEDULER_LOG_DIR=$DOLPHINSCHEDULER_HOME/$command/logs
  48. export STOP_TIMEOUT=5
  49. if [ ! -d "$DOLPHINSCHEDULER_LOG_DIR" ]; then
  50. mkdir $DOLPHINSCHEDULER_LOG_DIR
  51. fi
  52. pid=$DOLPHINSCHEDULER_HOME/$command/pid
  53. cd $DOLPHINSCHEDULER_HOME/$command
  54. if [ "$command" = "api-server" ]; then
  55. log=$DOLPHINSCHEDULER_HOME/api-server/logs/$command-$HOSTNAME.out
  56. elif [ "$command" = "master-server" ]; then
  57. log=$DOLPHINSCHEDULER_HOME/master-server/logs/$command-$HOSTNAME.out
  58. elif [ "$command" = "worker-server" ]; then
  59. log=$DOLPHINSCHEDULER_HOME/worker-server/logs/$command-$HOSTNAME.out
  60. elif [ "$command" = "alert-server" ]; then
  61. log=$DOLPHINSCHEDULER_HOME/alert-server/logs/$command-$HOSTNAME.out
  62. elif [ "$command" = "standalone-server" ]; then
  63. log=$DOLPHINSCHEDULER_HOME/standalone-server/logs/$command-$HOSTNAME.out
  64. else
  65. echo "Error: No command named '$command' was found."
  66. exit 1
  67. fi
  68. state=""
  69. function get_server_running_status() {
  70. state="STOP"
  71. if [ -f $pid ]; then
  72. TARGET_PID=`cat $pid`
  73. if [[ $(ps -p "$TARGET_PID" -o comm=) =~ "bash" ]]; then
  74. state="RUNNING"
  75. fi
  76. fi
  77. }
  78. case $startStop in
  79. (start)
  80. # if server is already started, cancel this launch
  81. get_server_running_status
  82. if [[ $state == "RUNNING" ]]; then
  83. echo "$command running as process $TARGET_PID. Stop it first."
  84. exit 1
  85. fi
  86. echo starting $command, logging to $DOLPHINSCHEDULER_LOG_DIR
  87. overwrite_server_env "${command}"
  88. nohup /bin/bash "$DOLPHINSCHEDULER_HOME/$command/bin/start.sh" > $log 2>&1 &
  89. echo $! > $pid
  90. ;;
  91. (stop)
  92. if [ -f $pid ]; then
  93. TARGET_PID=`cat $pid`
  94. if kill -0 $TARGET_PID > /dev/null 2>&1; then
  95. echo stopping $command
  96. pkill -P $TARGET_PID
  97. sleep $STOP_TIMEOUT
  98. if kill -0 $TARGET_PID > /dev/null 2>&1; then
  99. echo "$command did not stop gracefully after $STOP_TIMEOUT seconds: killing with kill -9"
  100. pkill -P -9 $TARGET_PID
  101. fi
  102. else
  103. echo no $command to stop
  104. fi
  105. rm -f $pid
  106. else
  107. echo no $command to stop
  108. fi
  109. ;;
  110. (status)
  111. get_server_running_status
  112. if [[ $state == "STOP" ]]; then
  113. # font color - red
  114. state="[ \033[1;31m $state \033[0m ]"
  115. else
  116. # font color - green
  117. state="[ \033[1;32m $state \033[0m ]"
  118. fi
  119. echo -e "$command $state"
  120. ;;
  121. (*)
  122. echo $usage
  123. exit 1
  124. ;;
  125. esac
  126. echo "End $startStop $command."