mr-jobhistory-daemon.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. # Environment Variables
  18. #
  19. # HADOOP_JHS_LOGGER Hadoop JobSummary logger.
  20. # HADOOP_CONF_DIR Alternate conf dir. Default is ${HADOOP_MAPRED_HOME}/conf.
  21. # HADOOP_MAPRED_PID_DIR The pid files are stored. /tmp by default.
  22. # HADOOP_MAPRED_NICENESS The scheduling priority for daemons. Defaults to 0.
  23. ##
  24. usage="Usage: mr-jobhistory-daemon.sh [--config <conf-dir>] (start|stop) <mapred-command> "
  25. # if no args specified, show usage
  26. if [ $# -le 1 ]; then
  27. echo $usage
  28. exit 1
  29. fi
  30. bin=`dirname "${BASH_SOURCE-$0}"`
  31. bin=`cd "$bin"; pwd`
  32. DEFAULT_LIBEXEC_DIR="$bin"/../libexec
  33. HADOOP_LIBEXEC_DIR=${HADOOP_LIBEXEC_DIR:-$DEFAULT_LIBEXEC_DIR}
  34. if [ -e ${HADOOP_LIBEXEC_DIR}/mapred-config.sh ]; then
  35. . $HADOOP_LIBEXEC_DIR/mapred-config.sh
  36. fi
  37. # get arguments
  38. startStop=$1
  39. shift
  40. command=$1
  41. shift
  42. hadoop_rotate_log ()
  43. {
  44. log=$1;
  45. num=5;
  46. if [ -n "$2" ]; then
  47. num=$2
  48. fi
  49. if [ -f "$log" ]; then # rotate logs
  50. while [ $num -gt 1 ]; do
  51. prev=`expr $num - 1`
  52. [ -f "$log.$prev" ] && mv "$log.$prev" "$log.$num"
  53. num=$prev
  54. done
  55. mv "$log" "$log.$num";
  56. fi
  57. }
  58. if [ "$HADOOP_MAPRED_IDENT_STRING" = "" ]; then
  59. export HADOOP_MAPRED_IDENT_STRING="$USER"
  60. fi
  61. export HADOOP_MAPRED_HOME=${HADOOP_MAPRED_HOME:-${HADOOP_PREFIX}}
  62. export HADOOP_MAPRED_LOGFILE=mapred-$HADOOP_MAPRED_IDENT_STRING-$command-$HOSTNAME.log
  63. export HADOOP_MAPRED_ROOT_LOGGER=${HADOOP_MAPRED_ROOT_LOGGER:-INFO,RFA}
  64. export HADOOP_JHS_LOGGER=${HADOOP_JHS_LOGGER:-INFO,JSA}
  65. if [ -f "${HADOOP_CONF_DIR}/mapred-env.sh" ]; then
  66. . "${HADOOP_CONF_DIR}/mapred-env.sh"
  67. fi
  68. mkdir -p "$HADOOP_MAPRED_LOG_DIR"
  69. chown $HADOOP_MAPRED_IDENT_STRING $HADOOP_MAPRED_LOG_DIR
  70. if [ "$HADOOP_MAPRED_PID_DIR" = "" ]; then
  71. HADOOP_MAPRED_PID_DIR=/tmp
  72. fi
  73. HADOOP_OPTS="$HADOOP_OPTS -Dhadoop.id.str=$HADOOP_MAPRED_IDENT_STRING"
  74. log=$HADOOP_MAPRED_LOG_DIR/mapred-$HADOOP_MAPRED_IDENT_STRING-$command-$HOSTNAME.out
  75. pid=$HADOOP_MAPRED_PID_DIR/mapred-$HADOOP_MAPRED_IDENT_STRING-$command.pid
  76. HADOOP_MAPRED_STOP_TIMEOUT=${HADOOP_MAPRED_STOP_TIMEOUT:-5}
  77. # Set default scheduling priority
  78. if [ "$HADOOP_MAPRED_NICENESS" = "" ]; then
  79. export HADOOP_MAPRED_NICENESS=0
  80. fi
  81. case $startStop in
  82. (start)
  83. mkdir -p "$HADOOP_MAPRED_PID_DIR"
  84. if [ -f $pid ]; then
  85. if kill -0 `cat $pid` > /dev/null 2>&1; then
  86. echo $command running as process `cat $pid`. Stop it first.
  87. exit 1
  88. fi
  89. fi
  90. hadoop_rotate_log $log
  91. echo starting $command, logging to $log
  92. cd "$HADOOP_MAPRED_HOME"
  93. nohup nice -n $HADOOP_MAPRED_NICENESS "$HADOOP_MAPRED_HOME"/bin/mapred --config $HADOOP_CONF_DIR $command "$@" > "$log" 2>&1 < /dev/null &
  94. echo $! > $pid
  95. sleep 1; head "$log"
  96. ;;
  97. (stop)
  98. if [ -f $pid ]; then
  99. TARGET_PID=`cat $pid`
  100. if kill -0 $TARGET_PID > /dev/null 2>&1; then
  101. echo stopping $command
  102. kill $TARGET_PID
  103. sleep $HADOOP_MAPRED_STOP_TIMEOUT
  104. if kill -0 $TARGET_PID > /dev/null 2>&1; then
  105. echo "$command did not stop gracefully after $HADOOP_MAPRED_STOP_TIMEOUT seconds: killing with kill -9"
  106. kill -9 $TARGET_PID
  107. fi
  108. else
  109. echo no $command to stop
  110. fi
  111. else
  112. echo no $command to stop
  113. fi
  114. ;;
  115. (*)
  116. echo $usage
  117. exit 1
  118. ;;
  119. esac