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