maxwell 启停脚本

前言

我已经很久没有写过博客了,今天再次登录之后发现有了很多的关注和评论,让我感到我的文章的对大家还是有帮助的,以后会继续写下去的,进入主题。我使用maxwell也有很长时间的,之前都是手动敲命令来启动,随着平台越来越多,一个个去启动就有点麻烦,于是今天准备分享一个我写的maxwell 启动脚本。

maxwell

版本:1.29.1
| 目录结构

drwxr-xr-x 2 root root    4096 Dec 10 10:00 bin
-rw-r--r-- 1 root root     467 Dec 14 15:02 config-agent.properties
-rw-r--r-- 1 root root     574 Dec 10 10:00 config-caredaily.properties
-rw-r--r-- 1  501 games  25133 Dec 16  2020 config.md
-rw-r--r-- 1  501 games  11970 Dec  3  2020 config.properties.example
-rw-r--r-- 1  501 games  10259 Dec  3  2020 kinesis-producer-library.properties.example
drwxr-xr-x 3  501 games  12288 Dec 23  2020 lib
-rw-r--r-- 1  501 games    548 Dec  3  2020 LICENSE
-rw-r--r-- 1  501 games    470 Dec  3  2020 log4j2.xml
-rw------- 1 root root  168860 Dec 25 14:53 nohup.out
-rw-r--r-- 1  501 games   3328 Dec 23  2020 quickstart.md
-rw-r--r-- 1  501 games   1429 Dec 23  2020 README.md

其中 “config-agent.properties” 和 “config-caredaily.properties” 都是任务配置,监控着不同的数据库。

注:具体的配置不在这里细讲

编写脚本

注意事项

注:千万不要取名为 maxwell 或 maxwell.sh
原因1: bin目录下已有名为 maxwell,尽量避免重名,以免出现问题排查麻烦。

[root@data01 maxwell-1.29.1]# ls bin/
maxwell  maxwell-benchmark  maxwell-bootstrap  maxwell-docker

原因2: 当使用 kill maxwell 会误将你写的脚本kill掉,尽量避免重名。

vim mxw.sh

我这里取名叫“mxw.sh” 当然也可以取他名称

#!/bin/bash

# axwell 地址
MAXWELL_HOME=/opt/module/maxwell-1.29.1

# maxwell 有哪些服务
names=(agent caredaily)


# 判断状态是否运行中
status_maxwell(){
        pid=`ps -aux|grep com.zendesk.maxwell.Maxwell |grep config-$1 | grep -v grep | awk '{print $2}'`
        echo $pid
        return $pid
}

# 启动
start_maxwell(){
        for name in ${names[@]}
        do

                pid=`status_maxwell $name`
                if [[ -z $pid ]]; then
                        echo "准备启动 $name..."
                        $MAXWELL_HOME/bin/maxwell --config $MAXWELL_HOME/config-$name.properties --daemon
                        sleep 5s
                else
                        echo "$name 已经启动,无需再启动"
                fi
        done
}

# 停止
stop_maxwell(){
        for name in ${names[@]}
        do
                pid=`status_maxwell $name`
                if [[ -z $pid ]];then
                        echo "$name 未启动,无需停止"
                else
                        echo "准备停止 $name...$pid"
                        kill -9 $pid
                fi

        done
}

case $1 in
"start")
        start_maxwell
        ;;
"stop")
        stop_maxwell
        ;;
"status")
        for name in ${names[@]}
        do
                pid=`status_maxwell $name`
                echo "$name pid>> $pid"
        done
        ;;
*)
        echo "参数输入有误!仅支持{start、stop、status}"
        ;;
esac

执行

启动

[root@data01 maxwell-1.29.1]# sh mxw.sh start
agent 已经启动,无需再启动
caredaily 已经启动,无需再启动

查看执行情况

[root@data01 maxwell-1.29.1]# sh mxw.sh status
agent pid>> 12429
caredaily pid>> 12729

关闭

[root@data01 maxwell-1.29.1]# sh mxw.sh stop
agent pid>> 12429
准备停止 agent ...12429
准备停止 caredaily ...12729

你可能感兴趣的:(maxwell 启停脚本)