dolphinscheduler-daemon.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. source "${DOLPHINSCHEDULER_HOME}/bin/env/dolphinscheduler_env.sh"
  47. export HOSTNAME=`hostname`
  48. export DOLPHINSCHEDULER_LOG_DIR=$DOLPHINSCHEDULER_HOME/$command/logs
  49. export STOP_TIMEOUT=5
  50. if [ ! -d "$DOLPHINSCHEDULER_LOG_DIR" ]; then
  51. mkdir $DOLPHINSCHEDULER_LOG_DIR
  52. fi
  53. pid=$DOLPHINSCHEDULER_HOME/$command/pid
  54. cd $DOLPHINSCHEDULER_HOME/$command
  55. if [ "$command" = "api-server" ]; then
  56. log=$DOLPHINSCHEDULER_HOME/api-server/logs/$command-$HOSTNAME.out
  57. elif [ "$command" = "master-server" ]; then
  58. log=$DOLPHINSCHEDULER_HOME/master-server/logs/$command-$HOSTNAME.out
  59. elif [ "$command" = "worker-server" ]; then
  60. log=$DOLPHINSCHEDULER_HOME/worker-server/logs/$command-$HOSTNAME.out
  61. elif [ "$command" = "alert-server" ]; then
  62. log=$DOLPHINSCHEDULER_HOME/alert-server/logs/$command-$HOSTNAME.out
  63. elif [ "$command" = "standalone-server" ]; then
  64. log=$DOLPHINSCHEDULER_HOME/standalone-server/logs/$command-$HOSTNAME.out
  65. else
  66. echo "Error: No command named '$command' was found."
  67. exit 1
  68. fi
  69. case $startStop in
  70. (start)
  71. echo starting $command, logging to $DOLPHINSCHEDULER_LOG_DIR
  72. overwrite_server_env "${command}"
  73. nohup /bin/bash "$DOLPHINSCHEDULER_HOME/$command/bin/start.sh" > $log 2>&1 &
  74. echo $! > $pid
  75. ;;
  76. (stop)
  77. if [ -f $pid ]; then
  78. TARGET_PID=`cat $pid`
  79. if kill -0 $TARGET_PID > /dev/null 2>&1; then
  80. echo stopping $command
  81. pkill -P $TARGET_PID
  82. sleep $STOP_TIMEOUT
  83. if kill -0 $TARGET_PID > /dev/null 2>&1; then
  84. echo "$command did not stop gracefully after $STOP_TIMEOUT seconds: killing with kill -9"
  85. pkill -P -9 $TARGET_PID
  86. fi
  87. else
  88. echo no $command to stop
  89. fi
  90. rm -f $pid
  91. else
  92. echo no $command to stop
  93. fi
  94. ;;
  95. (status)
  96. # more details about the status can be added later
  97. serverCount=`ps -ef | grep "$DOLPHINSCHEDULER_HOME" | grep "$CLASS" | grep -v "grep" | wc -l`
  98. state="STOP"
  99. # font color - red
  100. state="[ \033[1;31m $state \033[0m ]"
  101. if [[ $serverCount -gt 0 ]];then
  102. state="RUNNING"
  103. # font color - green
  104. state="[ \033[1;32m $state \033[0m ]"
  105. fi
  106. echo -e "$command $state"
  107. ;;
  108. (*)
  109. echo $usage
  110. exit 1
  111. ;;
  112. esac
  113. echo "End $startStop $command."