Hadoop学习笔记(一)HBase脚本分析(二)hbase-daemon.sh

Hadoop学习笔记(一)HBase脚本分析(二)hbase-daemon.sh。

  1. usage="Usage: hbase-daemon.sh [--config <conf-dir>]\  
  2.  (start|stop|restart) <hbase-command> \  
  3.  <args...>"  
  4.   
  5. # if no args specified, show usage   
  6. if [ $# -le 1 ]; then   
  7.   echo $usage  
  8.   exit 1  
  9. fi  
  10.   
  11. bin=`dirname "${BASH_SOURCE-$0}"`  
  12. bin=`cd "$bin">/dev/null; pwd`  
  13.   
  14. "$bin"/hbase-config.sh  
  15.   
  16. # get arguments   
  17. startStop=$1        //获取第一个参数  
  18. shift  
  19.   
  20. command=$1          //获取第二个参数  
  21. shift  
  22.   
  23. #把现存的日志号依次加一   
  24. hbase_rotate_log ()  
  25. {  
  26.     log=$1;  
  27.     num=5;  
  28.     if [ -n "$2" ]; then  
  29.     num=$2  
  30.     fi  
  31.     if [ -f "$log" ]; then # rotate logs   
  32.     while [ $num -gt 1 ]; do  
  33.         prev=`expr $num - 1`  
  34.         [ -f "$log.$prev" ] && mv -f "$log.$prev" "$log.$num"  
  35.         num=$prev  
  36.     done  
  37.     mv -f "$log" "$log.$num";  
  38.     fi  
  39. }  
  40.   
  41. wait_until_done ()  
  42. {  
  43.     p=$1  
  44.     cnt=${HBASE_SLAVE_TIMEOUT:-300}  
  45.     origcnt=$cnt  
  46.     while kill -0 $p > /dev/null 2>&1; do  
  47.       if [ $cnt -gt 1 ]; then  
  48.         cnt=`expr $cnt - 1`  
  49.         sleep 1  
  50.       else  
  51.         echo "Process did not complete after $origcnt seconds, killing."  
  52.         kill -9 $p  
  53.         exit 1  
  54.       fi  
  55.     done  
  56.     return 0  
  57. }  
  58.   
  59. # get log directory   
  60. if [ "$HBASE_LOG_DIR" = "" ]; then  
  61.   export HBASE_LOG_DIR="$HBASE_HOME/logs"  
  62. fi  
  63. mkdir -p "$HBASE_LOG_DIR"  
  64.   
  65. if [ "$HBASE_PID_DIR" = "" ]; then  
  66.   HBASE_PID_DIR=/tmp  
  67. fi  
  68.   
  69. if [ "$HBASE_IDENT_STRING" = "" ]; then  
  70.   export HBASE_IDENT_STRING="$USER"  
  71. fi  
  72.   
  73. # Some variables   
  74. # Work out java location so can print version into log.   
  75. #获取JAVA命令path   
  76. if [ "$JAVA_HOME" != "" ]; then  
  77.   #echo "run java in $JAVA_HOME"   
  78.   JAVA_HOME=$JAVA_HOME  
  79. fi  
  80. if [ "$JAVA_HOME" = "" ]; then  
  81.   echo "Error: JAVA_HOME is not set."  
  82.   exit 1  
  83. fi  
  84. JAVA=$JAVA_HOME/bin/java  
  85.   
  86. export HBASE_LOGFILE=hbase-$HBASE_IDENT_STRING-$command-$HOSTNAME.log  
  87. export HBASE_ROOT_LOGGER="INFO,DRFA"  
  88. logout=$HBASE_LOG_DIR/hbase-$HBASE_IDENT_STRING-$command-$HOSTNAME.out      //定义日志输出  
  89. loglog="${HBASE_LOG_DIR}/${HBASE_LOGFILE}"                                  //定义日志文件  
  90. pid=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.pid                   //定义进程文件  
  91.   
  92. # Set default scheduling priority   
  93. if [ "$HBASE_NICENESS" = "" ]; then  
  94.     export HBASE_NICENESS=0  
  95. fi  
  96.   
  97. #根据第一参数执行命令   
  98. case $startStop in  
  99.   
  100.   (start)  
  101.     mkdir -p "$HBASE_PID_DIR"  
  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.     hbase_rotate_log $logout             //调用函数  
  110.     echo starting $command, logging to $logout  
  111.     # Add to the command log file vital stats on our environment.   
  112.     echo "`date` Starting $command on `hostname`" >> $loglog  
  113.     echo "`ulimit -a`" >> $loglog 2>&1                        //当前所有资源限制输出到日志  
  114.     nohup nice -n $HBASE_NICENESS "$HBASE_HOME"/bin/hbase \  
  115.         --config "${HBASE_CONF_DIR}" \  
  116.         $command "$@" $startStop > "$logout" 2>&1 < /dev/null &       //根据command启动hbase进程,把其中的错误定向到标准输出  
  117.     echo $! > $pid                                                    //此处不明,待查验  
  118.                                                                
  119.     sleep 1; head "$logout"                                           //显示日志输出的开头  
  120.     ;;  
  121.   
  122.   (stop)  
  123.     if [ -f $pid ]; then  
  124.       # kill -0 == see if the PID exists    
  125.       if kill -0 `cat $pid` > /dev/null 2>&1; then  
  126.         echo -n stopping $command  
  127.         echo "`date` Killing $command" >> $loglog  
  128.         kill `cat $pid` > /dev/null 2>&1  
  129.         while kill -0 `cat $pid` > /dev/null 2>&1; do  
  130.           echo -n "."  
  131.           sleep 1;  
  132.         done  
  133.         rm $pid  
  134.         echo  
  135.       else  
  136.         retval=$?  
  137.         echo no $command to stop because kill -0 of pid `cat $pid` failed with status $retval  
  138.       fi  
  139.     else  
  140.       echo no $command to stop because no pid file $pid  
  141.     fi  
  142.     ;;  
  143.   
  144.   (restart)  
  145.     thiscmd=$0  
  146.     args=$@  
  147.     # stop the command   
  148.     $thiscmd --config "${HBASE_CONF_DIR}" stop $command $args &  
  149.     wait_until_done $!  
  150.     # wait a user-specified sleep period   
  151.     sp=${HBASE_RESTART_SLEEP:-3}  
  152.     if [ $sp -gt 0 ]; then  
  153.       sleep $sp  
  154.     fi  
  155.     # start the command   
  156.     $thiscmd --config "${HBASE_CONF_DIR}" start $command $args &  
  157.     wait_until_done $!  
  158.     ;;  
  159.   
  160.   (*)  
  161.     echo $usage  
  162.     exit 1  
  163.     ;;  
  164.   
  165. esac  
  166.   
  167.    

注:

Special Parameters
       The shell treats several parameters specially.  These parameters may only be referenced; assignment to them is not allowed.
       *      Expands to the positional parameters, starting from one.  When the expansion occurs within double quotes, it expands
              to a single word with the value of each parameter separated by the first character  of  the  IFS  special  variable.
              That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable.  If IFS
              is unset, the parameters are separated by spaces.  If IFS is null, the parameters  are  joined  without  intervening
              separators.
       @      Expands  to  the  positional  parameters,  starting  from one.  When the expansion occurs within double quotes, each
              parameter expands to a separate word.  That is, "$@" is equivalent to "$1" "$2" ...  If the double-quoted  expansion
              occurs  within  a word, the expansion of the first parameter is joined with the beginning part of the original word,
              and the expansion of the last parameter is joined with the last part of the original word.  When there are no  posi‐
              tional parameters, "$@" and $@ expand to nothing (i.e., they are removed).
       #      Expands to the number of positional parameters in decimal.
       ?      Expands to the exit status of the most recently executed foreground pipeline.
       -      Expands  to  the  current option flags as specified upon invocation, by the set builtin command, or those set by the
              shell itself (such as the -i option).
       $      Expands to the process ID of the shell.  In a () subshell, it expands to the process ID of the  current  shell,  not
              the subshell.
       !      Expands to the process ID of the most recently executed background (asynchronous) command.
       0      Expands  to  the name of the shell or shell script.  This is set at shell initialization.  If bash is invoked with a
              file of commands, $0 is set to the name of that file.  If bash is started with the -c option, then $0 is set to  the
              first  argument  after  the string to be executed, if one is present.  Otherwise, it is set to the file name used to
              invoke bash, as given by argument zero.
       _      At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed  in
              the  environment or argument list.  Subsequently, expands to the last argument to the previous command, after expan‐
              sion.  Also set to the full pathname used to invoke each command executed and placed in the environment exported  to
              that command.  When checking mail, this parameter holds the name of the mail file currently being checked.

 

 

 

相关推荐