usage="Usage: hbase-daemon.sh [--config <conf-dir>]\
(start|stop|restart) <hbase-command> \
<args...>"
# if no args specified, show usage
if [ $# -le 1 ]; then
echo $usage
exit 1
fi
bin=`dirname "${BASH_SOURCE-$0}"`
bin=`cd "$bin">/dev/null; pwd`
. "$bin"/hbase-config.sh
# get arguments
startStop=$1 //获取第一个参数
shift
command=$1 //获取第二个参数
shift
#把现存的日志号依次加一
hbase_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 -f "$log.$prev" "$log.$num"
num=$prev
done
mv -f "$log" "$log.$num";
fi
}
wait_until_done ()
{
p=$1
cnt=${HBASE_SLAVE_TIMEOUT:-300}
origcnt=$cnt
while kill -0 $p > /dev/null 2>&1; do
if [ $cnt -gt 1 ]; then
cnt=`expr $cnt - 1`
sleep 1
else
echo "Process did not complete after $origcnt seconds, killing."
kill -9 $p
exit 1
fi
done
return 0
}
# get log directory
if [ "$HBASE_LOG_DIR" = "" ]; then
export HBASE_LOG_DIR="$HBASE_HOME/logs"
fi
mkdir -p "$HBASE_LOG_DIR"
if [ "$HBASE_PID_DIR" = "" ]; then
HBASE_PID_DIR=/tmp
fi
if [ "$HBASE_IDENT_STRING" = "" ]; then
export HBASE_IDENT_STRING="$USER"
fi
# Some variables
# Work out java location so can print version into log.
#获取JAVA命令path
if [ "$JAVA_HOME" != "" ]; then
#echo "run java in $JAVA_HOME"
JAVA_HOME=$JAVA_HOME
fi
if [ "$JAVA_HOME" = "" ]; then
echo "Error: JAVA_HOME is not set."
exit 1
fi
JAVA=$JAVA_HOME/bin/java
export HBASE_LOGFILE=hbase-$HBASE_IDENT_STRING-$command-$HOSTNAME.log
export HBASE_ROOT_LOGGER="INFO,DRFA"
logout=$HBASE_LOG_DIR/hbase-$HBASE_IDENT_STRING-$command-$HOSTNAME.out //定义日志输出
loglog="${HBASE_LOG_DIR}/${HBASE_LOGFILE}" //定义日志文件
pid=$HBASE_PID_DIR/hbase-$HBASE_IDENT_STRING-$command.pid //定义进程文件
# Set default scheduling priority
if [ "$HBASE_NICENESS" = "" ]; then
export HBASE_NICENESS=0
fi
#根据第一参数执行命令
case $startStop in
(start)
mkdir -p "$HBASE_PID_DIR"
if [ -f $pid ]; then //判断进程是否已启动,若启动则提示
if kill -0 `cat $pid` > /dev/null 2>&1; then
echo $command running as process `cat $pid`. Stop it first.
exit 1
fi
fi
hbase_rotate_log $logout //调用函数
echo starting $command, logging to $logout
# Add to the command log file vital stats on our environment.
echo "`date` Starting $command on `hostname`" >> $loglog
echo "`ulimit -a`" >> $loglog 2>&1 //当前所有资源限制输出到日志
nohup nice -n $HBASE_NICENESS "$HBASE_HOME"/bin/hbase \
--config "${HBASE_CONF_DIR}" \
$command "$@" $startStop > "$logout" 2>&1 < /dev/null & //根据command启动hbase进程,把其中的错误定向到标准输出
echo $! > $pid //获取最近异步进程的pid号
sleep 1; head "$logout" //显示日志输出的开头
;;
(stop)
if [ -f $pid ]; then
# kill -0 == see if the PID exists
if kill -0 `cat $pid` > /dev/null 2>&1; then
echo -n stopping $command
echo "`date` Killing $command" >> $loglog
kill `cat $pid` > /dev/null 2>&1
while kill -0 `cat $pid` > /dev/null 2>&1; do
echo -n "."
sleep 1;
done
rm $pid
echo
else
retval=$?
echo no $command to stop because kill -0 of pid `cat $pid` failed with status $retval
fi
else
echo no $command to stop because no pid file $pid
fi
;;
(restart)
thiscmd=$0
args=$@
# stop the command
$thiscmd --config "${HBASE_CONF_DIR}" stop $command $args &
wait_until_done $!
# wait a user-specified sleep period
sp=${HBASE_RESTART_SLEEP:-3}
if [ $sp -gt 0 ]; then
sleep $sp
fi
# start the command
$thiscmd --config "${HBASE_CONF_DIR}" start $command $args &
wait_until_done $!
;;
(*)
echo $usage
exit 1
;;
esac
注:
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.