mr-jobhistory-daemon.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. # get arguments
  33. startStop=$1
  34. shift
  35. command=$1
  36. shift
  37. hadoop_rotate_log ()
  38. {
  39. log=$1;
  40. num=5;
  41. if [ -n "$2" ]; then
  42. num=$2
  43. fi
  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. if [ "$HADOOP_MAPRED_IDENT_STRING" = "" ]; then
  54. export HADOOP_MAPRED_IDENT_STRING="$USER"
  55. fi
  56. export HADOOP_MAPRED_HOME=${HADOOP_MAPRED_HOME:-${HADOOP_PREFIX}}
  57. export HADOOP_MAPRED_LOGFILE=mapred-$HADOOP_MAPRED_IDENT_STRING-$command-$HOSTNAME.log
  58. export HADOOP_MAPRED_ROOT_LOGGER=${HADOOP_MAPRED_ROOT_LOGGER:-INFO,RFA}
  59. export HADOOP_JHS_LOGGER=${HADOOP_JHS_LOGGER:-INFO,JSA}
  60. DEFAULT_LIBEXEC_DIR="$bin"/../libexec
  61. HADOOP_LIBEXEC_DIR=${HADOOP_LIBEXEC_DIR:-$DEFAULT_LIBEXEC_DIR}
  62. . $HADOOP_LIBEXEC_DIR/mapred-config.sh
  63. if [ -f "${HADOOP_CONF_DIR}/mapred-env.sh" ]; then
  64. . "${HADOOP_CONF_DIR}/mapred-env.sh"
  65. fi
  66. mkdir -p "$HADOOP_MAPRED_LOG_DIR"
  67. chown $HADOOP_MAPRED_IDENT_STRING $HADOOP_MAPRED_LOG_DIR
  68. if [ "$HADOOP_MAPRED_PID_DIR" = "" ]; then
  69. HADOOP_MAPRED_PID_DIR=/tmp
  70. fi
  71. HADOOP_OPTS="$HADOOP_OPTS -Dhadoop.id.str=$HADOOP_MAPRED_IDENT_STRING"
  72. log=$HADOOP_MAPRED_LOG_DIR/mapred-$HADOOP_MAPRED_IDENT_STRING-$command-$HOSTNAME.out
  73. pid=$HADOOP_MAPRED_PID_DIR/mapred-$HADOOP_MAPRED_IDENT_STRING-$command.pid
  74. HADOOP_MAPRED_STOP_TIMEOUT=${HADOOP_MAPRED_STOP_TIMEOUT:-5}
  75. # Set default scheduling priority
  76. if [ "$HADOOP_MAPRED_NICENESS" = "" ]; then
  77. export HADOOP_MAPRED_NICENESS=0
  78. fi
  79. case $startStop in
  80. (start)
  81. mkdir -p "$HADOOP_MAPRED_PID_DIR"
  82. if [ -f $pid ]; then
  83. if kill -0 `cat $pid` > /dev/null 2>&1; then
  84. echo $command running as process `cat $pid`. Stop it first.
  85. exit 1
  86. fi
  87. fi
  88. hadoop_rotate_log $log
  89. echo starting $command, logging to $log
  90. cd "$HADOOP_MAPRED_HOME"
  91. nohup nice -n $HADOOP_MAPRED_NICENESS "$HADOOP_MAPRED_HOME"/bin/mapred --config $HADOOP_CONF_DIR $command "$@" > "$log" 2>&1 < /dev/null &
  92. echo $! > $pid
  93. sleep 1; head "$log"
  94. ;;
  95. (stop)
  96. if [ -f $pid ]; then
  97. TARGET_PID=`cat $pid`
  98. if kill -0 $TARGET_PID > /dev/null 2>&1; then
  99. echo stopping $command
  100. kill $TARGET_PID
  101. sleep $HADOOP_MAPRED_STOP_TIMEOUT
  102. if kill -0 $TARGET_PID > /dev/null 2>&1; then
  103. echo "$command did not stop gracefully after $HADOOP_MAPRED_STOP_TIMEOUT seconds: killing with kill -9"
  104. kill -9 $TARGET_PID
  105. fi
  106. else
  107. echo no $command to stop
  108. fi
  109. else
  110. echo no $command to stop
  111. fi
  112. ;;
  113. (*)
  114. echo $usage
  115. exit 1
  116. ;;
  117. esac