脚本-01

1. redis启停脚本,redis_start_stop.sh

#!/bin/bash

REDIS_PORT=6379
SENTINEL_PORT=26379
IP=`(ifconfig|grep inet|egrep -v "127.0.0.1"|awk '{print $2}')`

redis_start(){

    process_num=`(ps -ef |grep "redis-server"|egrep -v grep|wc -l)`
    if [ $process_num -eq 0 ];then
        redis-server /home/apps/redis/etc/redis.conf &>/dev/null
        if [ $? -eq 0 ];then
            process_id=`(ps -ef |grep "redis-server"|egrep -v grep|awk '{print $2}')`
            echo "Redis start success. Process id is $process_id"
        else
            echo "Redis start failed. Please check logfile."
        fi
    else
        process_id=`(ps -ef |grep "redis-server"|egrep -v grep|awk '{print $2}')`
        echo "redis server is running. please check process and the process id is $process_id"
    fi
}

redis_stop(){

    process_num=`(ps -ef |grep "redis-server"|egrep -v grep|wc -l)`
    if [ $process_num -ne 0 ];then
        redis-cli -h $IP -p $REDIS_PORT shutdown &>/dev/null
        process_num=`(ps -ef |grep "redis-server"|egrep -v grep|wc -l)`
        if [ $process_num -eq 0 ];then
            echo "redis server stopped success."
        else
            process_id=`(ps -ef |grep "redis-server"|egrep -v grep|awk '{print $2}')`
            echo "redis server stopped failed. the redis server process id is $process_id"
        fi
    else
        echo "redis server is not running."
    fi
}

case $1 in
    "start")
        redis_start
    ;;
    "stop")
        redis_stop
    ;;
    "restart")
        redis_stop
        sleep 3
        redis_start
    ;;
    *)
        echo "WARN: arg error.Please choose the right arg."
        echo "eg: start. stop. restart."
    ;;
esac

2.哨兵启停,sentinel_start_stop.sh

#!/bin/bash

SENTINEL_PORT=26379
IP=`(ifconfig|grep inet|egrep -v "127.0.0.1"|awk '{print $2}')`

sentinel_start(){

    process_num=`(ps -ef |grep "redis-sentinel"|egrep -v grep|wc -l)`
    if [ $process_num -eq 0 ];then
        redis-sentinel /home/apps/redis/etc/sentinel.conf &>/dev/null
        if [ $? -eq 0 ];then
            process_id=`(ps -ef |grep "redis-sentinel"|egrep -v grep|awk '{print $2}')`
            echo "sentinel start success. process id is $process_id"
        else
            echo "sentinel start failed. please check logfile."
        fi
    else
        process_id=`(ps -ef |grep "redis-sentinel"|egrep -v grep|awk '{print $2}')`
        echo "sentinel server is running. please check process and the process id is $process_id"
    fi

}

sentinel_stop(){

    process_num=`(ps -ef |grep "redis-sentinel"|egrep -v grep|wc -l)`
    if [ $process_num -ne 0 ];then
        redis-cli -h $IP -p $SENTINEL_PORT shutdown &>/dev/null
        process_num=`(ps -ef |grep "redis-sentinel"|egrep -v grep|wc -l)`
        if [ $process_num -eq 0 ];then
            echo "sentinel server stopped success."
        else
            process_id=`(ps -ef |grep "redis-sentinel"|egrep -v grep|awk '{print $2}')`
            echo "sentinel server stopped failed. the sentinel server process id is $process_id"
        fi
    else
        echo "sentinel server is not running."
    fi

}

case $1 in
    "start")
        sentinel_start
    ;;
    "stop")
        sentinel_stop
    ;;
    "restart")
        sentinel_stop
        sleep 3
        sentinel_start
    ;;
    *)
        echo "WARN: arg error.Please choose the right arg."
        echo "eg: start. stop. restart."
    ;;
esac

3.zk启停脚本 zk_start_stop.sh

下述内容因在单台测试部署集群,故与实际有所差异,三节点部署将for循环去掉即可

#!/bin/bash

SERVER=zookeeper

EXECUTE_ARG=$1

zkstart(){

    for i in 01 02 03
    do
        process_num=`(ps -ef |grep "$SERVER-$i"|egrep -v grep|wc -l)`
        if [ $process_num -eq 0 ];then
            cd /home/apps/$SERVER-$i/bin && ./zkServer.sh start &>/dev/null
#/home/apps/$SERVER-$i/bin/zkServer.sh start &>/dev/null
process_id=`(ps -ef |grep "$SERVER-$i"|egrep -v grep |awk '{print $2}')` if [ $? -eq 0 ];then echo "$SERVER-$i start successed. Process id is $process_id" else echo "$SERVER-$i start failed,please check logfile." fi else process_id=`(ps -ef |grep "$SERVER-$i"|egrep -v grep |awk '{print $2}')` echo "$SERVER-$i is running. process id is $process_id" fi done } zkstop(){ for i in 01 02 03 do process_num=`(ps -ef |grep "$SERVER-$i"|egrep -v grep|wc -l)` if [ $process_num -ne 0 ];then cd /home/apps/$SERVER-$i/bin && ./zkServer.sh stop &>/dev/null process_num=`(ps -ef |grep "$SERVER-$i"|egrep -v grep|wc -l)` if [ $process_num -eq 0 ];then echo "$SERVER-$i stop successed." else echo "$SERVER-$i stop failed,please check $SERVER-$i process." fi else echo "$SERVER-$i is not running." fi done } case $1 in "start") zkstart ;; "stop") zkstop ;; "restart") zkstop sleep 3 zkstart ;; *) echo "WARN: arg error.Please choose the right arg." echo "eg: start, stop, restart." ;; esac

多机器集群部署启停脚本

#!/bin/bash

SERVER=zookeeper

#EXECUTE_ARG=$1

zkstart(){

    process_num=`(ps -ef |grep "$SERVER"|egrep -v grep|wc -l)`
    if [ $process_num -eq 0 ];then
        cd /home/apps/$SERVER/bin && ./zkServer.sh start &>/dev/null       
        #/home/apps/$SERVER/bin/zkServer.sh start &>/dev/null
        if [ $? -eq 0 ];then
            process_id=`(ps -ef |grep "$SERVER"|egrep -v grep |awk '{print $2}')` 
            echo "$SERVER start successed. Process id is $process_id"
        else
            echo "$SERVER start failed,please check logfile."
        fi
    else
        process_id=`(ps -ef |grep "$SERVER"|egrep -v grep |awk '{print $2}')`
        echo "$SERVER is running. process id is $process_id"

}
zkstop(){

    process_num=`(ps -ef |grep "$SERVER"|egrep -v grep|wc -l)`
    if [ $process_num -ne 0 ];then
        cd /home/apps/$SERVER/bin && ./zkServer.sh stop &>/dev/null
        process_num=`(ps -ef |grep "$SERVER"|egrep -v grep|wc -l)`
        if [ $process_num -eq 0 ];then
            echo "$SERVER stop successed."
        else
            echo "$SERVER stop failed,please check $SERVER process."
        fi
    else
        echo "$SERVER is not running."

}
case $1 in
    "start")
        zkstart
        ;;
    "stop")
        zkstop
        ;;
    "restart")
        zkstop
        sleep 3
        zkstart
        ;;
    *)
        echo "WARN: arg error.Please choose the right arg."
        echo "eg: start, stop, restart."
        ;;
esac

4.Kafka启停脚本,kafka_start_stop.sh

因在单台测试部署集群,故与实际有所差异,三节点部署将for循环去掉即可

#!/bin/bash

SERVER=kafka

kafka_start(){

    for i in 01 02 03
    do
        process_num=`(ps -ef |grep "$SERVER-$i"|egrep -v grep |wc -l)`
        if [ $process_num -eq 0 ];then
            /home/apps/$SERVER-$i/bin/kafka-server-start.sh -daemon /home/apps/$SERVER-$i/config/server.properties &>/dev/null
            process_id=`(ps -ef |grep "$SERVER-$i"|egrep -v grep |awk '{print $2}')`
            if [ $? -eq 0 ];then
                echo "$SERVER-$i start successed. Process id is $process_id"
            else
                echo "$SERVER-$i start failed,please check logfile."
            fi
        else
            process_id=`(ps -ef |grep "$SERVER-$i"|egrep -v grep |awk '{print $2}')`
            echo "$SERVER-$i is running. process id is $process_id"
        fi
    done
}

kafka_stop(){

    process_num=`(ps -ef |grep kafka|egrep -v "grep|$0"|wc -l)`
    if [ $process_num -eq 0 ];then
        echo "kafka is not running."
    else
        /home/apps/kafka-01/bin/kafka-server-stop.sh &>/dev/null
        sleep 3
        process_num=`(ps -ef |grep kafka|egrep -v "grep|$0"|wc -l)`
        process_id=`(ps -ef |grep "$SERVER"|egrep -v grep |awk '{print $2}')`
        if [ $process_num -eq 0 ];then
            echo "kafka stop successed."
        else
            echo "kafka stop failed. process id is $process_id."
        fi
    fi
}

case $1 in
    "start")
        kafka_start
    ;;
    "stop")
        kafka_stop
    ;;
    "restart")
        kafka_stop
        sleep 3
        kafka_start
    ;;
    *)
        echo "WARN: arg error.Please choose the right arg."
        echo "eg: start, stop, restart."
        ;;
esac

 三节点部署集群

#!/bin/bash

SERVER=kafka

kafka_start(){

    process_num=`(ps -ef |grep "$SERVER"|egrep -v grep |wc -l)`
    if [ $process_num -eq 0 ];then
        /home/apps/$SERVER/bin/kafka-server-start.sh -daemon /home/apps/$SERVER/config/server.properties &>/dev/null
        if [ $? -eq 0 ];then
        process_id=`(ps -ef |grep "$SERVER"|egrep -v grep |awk '{print $2}')`
            echo "$SERVER start successed. Process id is $process_id"
        else
            echo "$SERVER start failed,please check logfile."
        fi
    else
        process_id=`(ps -ef |grep "$SERVER"|egrep -v grep |awk '{print $2}')`
        echo "$SERVER is running. process id is $process_id"

}

kafka_stop(){

    process_num=`(ps -ef |grep kafka|egrep -v "grep|$0"|wc -l)`
    if [ $process_num -eq 0 ];then
        echo "kafka is not running."
    else
        /home/apps/kafka/bin/kafka-server-stop.sh &>/dev/null
        sleep 3
        process_num=`(ps -ef |grep kafka|egrep -v "grep|$0"|wc -l)`
        process_id=`(ps -ef |grep "$SERVER"|egrep -v grep |awk '{print $2}')`
        if [ $process_num -eq 0 ];then
            echo "kafka stop successed."
        else
            echo "kafka stop failed. process id is $process_id."
        fi
    fi
}

case $1 in
    "start")
        kafka_start
    ;;
    "stop")
        kafka_stop
    ;;
    "restart")
        kafka_stop
        sleep 3
        kafka_start
    ;;
    *)
        echo "WARN: arg error.Please choose the right arg."
        echo "eg: start, stop, restart."
        ;;
esac

 

posted @ 2023-07-11 14:36  一窗明月半帘风  阅读(29)  评论(0编辑  收藏  举报