hadoop启动之“hadoop-daemon.sh”详解

        今天看了一下启动文件“hadoop-daemon.sh”,仔细看了一下大概知道它的作用,使用“hadoop-daemon.sh“脚本启动和停止hadoop后台程序。它可以做到在A机器上启动”namenode“,B机器启动”secondarynamenode“ C机器启动”datanode“, ”tasktracker“,具体启动如下 :

 ./hadoop-daemon.sh start namenode
 ./hadoop-daemon.sh start secondarynamenode
 ./hadoop-daemon.sh start jobtracker
 ./hadoop-daemon.sh start datanode
 ./hadoop-daemon.sh start tasktracker

如果要停止可以运行如下命令

 ./hadoop-daemon.sh stop namenode
 ./hadoop-daemon.sh stop secondarynamenode
 ./hadoop-daemon.sh stop jobtracker
 ./hadoop-daemon.sh stop datanode
 ./hadoop-daemon.sh stop tasktracker


下面是是代码分析,把主要部分都写了注释,代码不多,比较容易看懂,但有一处没看懂

# Runs a Hadoop command as a daemon.
#
# Environment Variables
#
#   HADOOP_CONF_DIR  Alternate conf dir. Default is ${HADOOP_HOME}/conf.
#   HADOOP_LOG_DIR   Where log files are stored.  PWD by default.
#   HADOOP_MASTER    host:path where hadoop code should be rsync'd from
#   HADOOP_PID_DIR   The pid files are stored. /tmp by default.
#   HADOOP_IDENT_STRING   A string representing this instance of hadoop. $USER by default
#   HADOOP_NICENESS The scheduling priority for daemons. Defaults to 0.
##

usage="Usage: hadoop-daemon.sh [--config ] [--hosts hostlistfile] (start|stop) "
# 第一个参数是下面的“startStop”是"start" or "end"
# 第二个参数是“hadoop-command”
# hadoop-command:就是namenode|datanode|secondarynamenode|jobtracker|tasktracke

# if no args specified, show usage
if [ $# -le 1 ]; then
  echo $usage
  exit 1
fi

bin=`dirname "$0"`
bin=`cd "$bin"; pwd`

. "$bin"/hadoop-config.sh

# get arguments
startStop=$1
shift
command=$1
shift

# 日记文件默认一共5个,后缀是log,log.1,log.2,log.3,log4,log5, 
# 每次写日记的文件名都是.log, 但是上一次的.log.num变成.log.num+1,
# .log.5被.log.4覆盖后结束
hadoop_rotate_log ()
{
    log=$1;
    num=5;
if [ -n "$2" ]; then
num=$2
    fi
    if [ -f "$log" ]; then # rotate logs
while [ $num -gt 1 ]; do
prev=`expr $num - 1`
[ -f "$log.$prev" ] && mv "$log.$prev" "$log.$num"
num=$prev
done
mv "$log" "$log.$num";
    fi
}

if [ -f "${HADOOP_CONF_DIR}/hadoop-env.sh" ]; then
  . "${HADOOP_CONF_DIR}/hadoop-env.sh"
fi

# get log directory
if [ "$HADOOP_LOG_DIR" = "" ]; then
  export HADOOP_LOG_DIR="$HADOOP_HOME/logs"
fi
mkdir -p "$HADOOP_LOG_DIR"

if [ "$HADOOP_PID_DIR" = "" ]; then
  HADOOP_PID_DIR=/tmp
fi

if [ "$HADOOP_IDENT_STRING" = "" ]; then
  export HADOOP_IDENT_STRING="$USER"
fi

# some variables
export HADOOP_LOGFILE=hadoop-$HADOOP_IDENT_STRING-$command-$HOSTNAME.log
export HADOOP_ROOT_LOGGER="INFO,DRFA"
log=$HADOOP_LOG_DIR/hadoop-$HADOOP_IDENT_STRING-$command-$HOSTNAME.out
pid=$HADOOP_PID_DIR/hadoop-$HADOOP_IDENT_STRING-$command.pid

# Set default scheduling priority
if [ "$HADOOP_NICENESS" = "" ]; then
    export HADOOP_NICENESS=0
fi

case $startStop in

  (start)

    mkdir -p "$HADOOP_PID_DIR"

    # 如果已经启动就提示先关闭,然后程序退出
    if [ -f $pid ]; then
  # 假如$command是“namenode”,就先判断它是否启动,由于$pid是存放
  # “namenode”运行的进程号,所以可以通过kill -0 `cat $pid`判断
      if kill -0 `cat $pid` > /dev/null 2>&1; then
        echo $command running as process `cat $pid`.  Stop it first.
        exit 1
      fi
    fi

    # 不知道干啥???
    if [ "$HADOOP_MASTER" != "" ]; then
      echo rsync from $HADOOP_MASTER
      rsync -a -e ssh --delete --exclude=.svn --exclude='logs/*' --exclude='contrib/hod/logs/*' $HADOOP_MASTER/ "$HADOOP_HOME"
    fi

# 记录日志
    hadoop_rotate_log $log
    echo starting $command, logging to $log
    cd "$HADOOP_HOME"

# nice指令可以改变程序执行的优先权等级
# 下面这段代码是核心代码,怎么样启动hadoop
    nohup nice -n $HADOOP_NICENESS "$HADOOP_HOME"/bin/hadoop --config $HADOOP_CONF_DIR $command "$@" > "$log" 2>&1 < /dev/null &
    # 将刚刚启动的进程号写入“$pid”文件
echo $! > $pid
    sleep 1; head "$log"
    ;;

  (stop)

    if [ -f $pid ]; then
      if kill -0 `cat $pid` > /dev/null 2>&1; then
        echo stopping $command
# 通过“$pid”文件内容,也就是进程号来关闭
        kill `cat $pid`
      else
        echo no $command to stop
      fi
    else
      echo no $command to stop
    fi
    ;;

  (*)
    echo $usage
    exit 1
    ;;

esac

你可能感兴趣的:(hadoop,云计算,hadoop,command,string,kill,代码分析,logging)