yarn-daemon.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. # Runs a yarn command as a daemon.
  17. #
  18. # Environment Variables
  19. #
  20. # YARN_CONF_DIR Alternate conf dir. Default is ${YARN_HOME}/conf.
  21. # YARN_LOG_DIR Where log files are stored. PWD by default.
  22. # YARN_MASTER host:path where hadoop code should be rsync'd from
  23. # YARN_PID_DIR The pid files are stored. /tmp by default.
  24. # YARN_IDENT_STRING A string representing this instance of hadoop. $USER by default
  25. # YARN_NICENESS The scheduling priority for daemons. Defaults to 0.
  26. ##
  27. usage="Usage: yarn-daemon.sh [--config <conf-dir>] [--hosts hostlistfile] (start|stop) <yarn-command> "
  28. # if no args specified, show usage
  29. if [ $# -le 1 ]; then
  30. echo $usage
  31. exit 1
  32. fi
  33. bin=`dirname "${BASH_SOURCE-$0}"`
  34. bin=`cd "$bin"; pwd`
  35. DEFAULT_LIBEXEC_DIR="$bin"/../libexec
  36. HADOOP_LIBEXEC_DIR=${HADOOP_LIBEXEC_DIR:-$DEFAULT_LIBEXEC_DIR}
  37. . $HADOOP_LIBEXEC_DIR/yarn-config.sh
  38. # get arguments
  39. startStop=$1
  40. shift
  41. command=$1
  42. shift
  43. hadoop_rotate_log ()
  44. {
  45. log=$1;
  46. num=5;
  47. if [ -n "$2" ]; then
  48. num=$2
  49. fi
  50. if [ -f "$log" ]; then # rotate logs
  51. while [ $num -gt 1 ]; do
  52. prev=`expr $num - 1`
  53. [ -f "$log.$prev" ] && mv "$log.$prev" "$log.$num"
  54. num=$prev
  55. done
  56. mv "$log" "$log.$num";
  57. fi
  58. }
  59. if [ -f "${YARN_CONF_DIR}/yarn-env.sh" ]; then
  60. . "${YARN_CONF_DIR}/yarn-env.sh"
  61. fi
  62. if [ "$YARN_IDENT_STRING" = "" ]; then
  63. export YARN_IDENT_STRING="$USER"
  64. fi
  65. # get log directory
  66. if [ "$YARN_LOG_DIR" = "" ]; then
  67. export YARN_LOG_DIR="$YARN_HOME/logs"
  68. fi
  69. if [ ! -w "$YARN_LOG_DIR" ] ; then
  70. mkdir -p "$YARN_LOG_DIR"
  71. chown $YARN_IDENT_STRING $YARN_LOG_DIR
  72. fi
  73. if [ "$YARN_PID_DIR" = "" ]; then
  74. YARN_PID_DIR=/tmp
  75. fi
  76. # some variables
  77. export YARN_LOGFILE=yarn-$YARN_IDENT_STRING-$command-$HOSTNAME.log
  78. export YARN_ROOT_LOGGER=${YARN_ROOT_LOGGER:-INFO,RFA}
  79. log=$YARN_LOG_DIR/yarn-$YARN_IDENT_STRING-$command-$HOSTNAME.out
  80. pid=$YARN_PID_DIR/yarn-$YARN_IDENT_STRING-$command.pid
  81. # Set default scheduling priority
  82. if [ "$YARN_NICENESS" = "" ]; then
  83. export YARN_NICENESS=0
  84. fi
  85. case $startStop in
  86. (start)
  87. [ -w "$YARN_PID_DIR" ] || mkdir -p "$YARN_PID_DIR"
  88. if [ -f $pid ]; then
  89. if kill -0 `cat $pid` > /dev/null 2>&1; then
  90. echo $command running as process `cat $pid`. Stop it first.
  91. exit 1
  92. fi
  93. fi
  94. if [ "$YARN_MASTER" != "" ]; then
  95. echo rsync from $YARN_MASTER
  96. rsync -a -e ssh --delete --exclude=.svn --exclude='logs/*' --exclude='contrib/hod/logs/*' $YARN_MASTER/ "$YARN_HOME"
  97. fi
  98. hadoop_rotate_log $log
  99. echo starting $command, logging to $log
  100. cd "$YARN_HOME"
  101. nohup nice -n $YARN_NICENESS "$YARN_HOME"/bin/yarn --config $YARN_CONF_DIR $command "$@" > "$log" 2>&1 < /dev/null &
  102. echo $! > $pid
  103. sleep 1; head "$log"
  104. ;;
  105. (stop)
  106. if [ -f $pid ]; then
  107. if kill -0 `cat $pid` > /dev/null 2>&1; then
  108. echo stopping $command
  109. kill `cat $pid`
  110. else
  111. echo no $command to stop
  112. fi
  113. else
  114. echo no $command to stop
  115. fi
  116. ;;
  117. (*)
  118. echo $usage
  119. exit 1
  120. ;;
  121. esac