Shell脚本一键启动关闭HDFS&YARN&Hive&ZooKeeper&Hbase服务

文章目录

  • 一、启动服务
  • 二、关闭服务


一、启动服务

#!/bin/bash

HB="hbase HMaster HRegionServer start-hbase.sh"
ZK="zookeeper QuorumPeerMain zkServer.sh_start"
HV="hive RunJar RunJar nohup_hive_--service_?>~/hive2.log_2>&1_&"
YN="yarn NodeManager ResourceManager start-yarn.sh"
HD="dfs DataNode NameNode SecondaryNameNode start-dfs.sh"

function checkStart(){
     
    COUNT=0
    ARR=$@
    ARR=($ARR)
    SIZE=$(($#-2))
    for i in `seq 1 $SIZE`
    do
        V=`jps|grep ${
     ARR[$i]}`
        if [ "$V" ]
        then
            if [ $1 == "hive" ]
            then
                V=($V)
                COUNT=$((${
     #V[*]}/2))
                break
            else
            ((COUNT++))
            fi
        fi
    done

    if [ $COUNT -eq $SIZE ]
    then
        echo "start $1 successfully"
    else
        echo "fail to start $1"
        echo "fail to execute start-all.sh ... please check reason and retry again"
        exit 1
    fi
}

function start(){
     
    ARR=$@
    ARR=($ARR)
    echo "start $1 ..."
    OUTPUT=${
     ARR[$#-1]}
    OUTPUT="${OUTPUT//_/ }"
    if [[ $OUTPUT =~ ^nohup ]]
    then
        eval ${
     OUTPUT/\?/metastore}
        sleep 2s
        eval ${
     OUTPUT/\?/hiveserver2}
        sleep 6s
    else
        OUTPUT=`$OUTPUT`
    fi
    checkStart $@
}

start $HD
start $YN
start $HV
start $ZK
start $HB

运行结果如下:

[root@single shell]# ./start-all.sh 
start dfs ...
start dfs successfully
start yarn ...
start yarn successfully
start hive ...
start hive successfully
start zookeeper ...
JMX enabled by default
Using config: /opt/software/hadoop/zookeeper345/bin/../conf/zoo.cfg
start zookeeper successfully
start hbase ...
start hbase successfully
[root@single shell]# jps
12289 QuorumPeerMain
12817 HRegionServer
11860 RunJar
11270 DataNode
12009 RunJar
11179 NameNode
11659 ResourceManager
11755 NodeManager
13387 Jps
11422 SecondaryNameNode
12671 HMaster

二、关闭服务

#!/bin/bash

HB="hbase HMaster HRegionServer stop-hbase.sh"
ZK="zookeeper QuorumPeerMain zkServer.sh_stop>~/zk.log_2>&1"
HV="hive RunJar RunJar kill"
YN="yarn NodeManager ResourceManager stop-yarn.sh"
HD="dfs DataNode NameNode SecondaryNameNode stop-dfs.sh"

function checkStop(){
     
    STOP=0
    ARR=$@
    ARR=($ARR)
    for i in `seq 1 $(($#-2))`
    do
        V=`jps|grep ${
     ARR[$i]}`
        if [ "$V" ]
        then
            ((STOP++))
            break
        fi
    done

    if [ $STOP -eq 0 ]
    then
        echo "stop $1 successfully"
    else
        echo "fail to stop $1"
    fi
}

function stop(){
     
    ARR=$@
    ARR=($ARR)
    COUNT=0
    for i in `seq 1 $(($#-2))`
    do
        V=`jps|grep ${
     ARR[$i]}`
        if [ "$V" ]
        then
            echo "start to stop $1 ..."
            OUTPUT=${
     ARR[$#-1]}
            ((COUNT++))
            if [ $OUTPUT = "kill" ]
            then
                PID=`jps|grep RunJar|awk 'NR==1 {print $1}'`
                OUTPUT=`kill -9 $PID`
            else
                OUTPUT=${
     OUTPUT//_/ }
                OUTPUT=`eval $OUTPUT`
                break
            fi
        fi
    done

    if [ $COUNT -eq 0 ]
    then
        echo "no $1 to stop"
    else
        checkStop $@
    fi
}

stop $HB
stop $ZK
stop $HV
stop $YN
stop $HD

运行结果如下:

[root@single shell]# ./stop-all.sh 
start to stop hbase ...
stop hbase successfully
start to stop zookeeper ...
stop zookeeper successfully
start to stop hive ...
start to stop hive ...
stop hive successfully
start to stop yarn ...
stop yarn successfully
start to stop dfs ...
stop dfs successfully
[root@single shell]# jps
11067 Jps

你可能感兴趣的:(Shell,shell,脚本语言)