Linux自动打包脚本


概述

本文主要介绍如何使用一些linux脚本

linux启动项目脚本

启动脚本名startup.bash

#!/bin/bash
start_monitor() {
    nohup java -cp KafkaOffsetMonitor-assembly-0.4.1-SNAPSHOT.jar com.quantifind.kafka.offsetapp.OffsetGetterWeb \
    --offsetStorage kafka \
    --kafkaBrokers 131.10.10.50:9092,131.10.10.51:9092,131.10.10.52:9092 \
    --zk 131.10.10.50:2181,131.10.10.51:2181,131.10.10.52:2181 \
    --port 8090 \
    --refresh 30.seconds \
    --retain 7.days >monitor.out 2>&1 < /dev/null &
}

start_zk() {
    cd /opt/cluster/zookeeper
  # 调用另一个启动命令启动ZK
    bin/zkServer.sh start 
    sleep 5

    cd ->/dev/null
}

start_kafka() {
    cd /opt/cluster/kafka
  # 调用另一个启动命令启动KAFKA
    bin/kafka-server-start.sh -daemon config/server.properties
    sleep 10

    cd ->/dev/null
}

start_connect() {
    cd /opt/cluster/kafka
  # 调用另一个启动命令启动CONNECT
    bin/connect-distributed.sh -daemon config/connect-distributed.properties
    sleep 10

    cd ->/dev/null
}

# nohup命令主要用于重定向输出日志文件,且使程序在后台运行,默认输出文件为nohup.out
start_storm() {
    cd /opt/cluster/storm

    if read -n 1 -p '是否为 nimbus 主机?[Y/N]:' op; then
        if [[ $op == [Yy] ]]; then #判断输入是否为y
            echo -e "\n\e[0;31;1m nimbus is starting...\e[0m"
            nohup bin/storm nimbus >nimbus.out 2>&1 < /dev/null &
            sleep 5
            echo -e "\n\e[0;31;1m ui is starting...\e[0m"
            nohup bin/storm ui >ui.out 2>&1 < /dev/null &
            sleep 5
        elif [[ $op == [Nn] ]]; then
            echo -e "\n\e[0;31;1m supervisor is starting...\e[0m"
            nohup bin/storm supervisor >supervisor.out 2>&1 < /dev/null &
            sleep 5
        else
            echo -e "\n\e[0;33;1m input error,exit...\e[0m"
            return
        fi
    fi

    result=$(ps -ef|grep java|grep logviewer)
    if [ "x$result" == "x" ]; then        
        echo -e "\n\e[0;31;1m logviewer is starting...\e[0m"
        nohup bin/storm logviewer >logviewer.out 2>&1 < /dev/null &
        sleep 5
    fi

    cd ->/dev/null
}

start_top() {
    if read -n 1 -p 'Topology是否初次提交到Storm?[Y/N]:' op; then
        if [[ $op == [Nn] ]]; then
            echo -e "\n\e[0;31;1mExit\e[0m"
            return
        elif [[ $op == [Yy] ]]; then
            echo -e "\n\e[0;32;1mSubmitting\e[0m"
        else
            echo -e "\n\e[0;33;1mError\e[0m"
            return
        fi
    fi

    cd /opt/cluster/storm

    bin/storm kill ThingsShadowTopologyDistributed #调用storm kill命令结束ThingsShadowTopologyDistributed进程
    sleep 5
    bin/storm jar topology/iot-data-storm-controller-1.0.0.jar  com.company.ApplicationLoader --bootstrap-servers=131.10.10.202:9092,131.10.10.203:9092,131.10.10.204:9092

    cd ->/dev/null
}

start_mqtt() {
    cd apache-activemq-5.14.5
    bin/activemq start
    sleep 2

    cd ->/dev/null
}

start_mqtt_mosqtuitto() {
    cd ../mosquitto-1.4.11/sbin
    ./mosquitto -c mosquitto.conf -d
    sleep 2

    cd ->/dev/null
}

start_mongodb() {
    systemctl start mongod
    sleep 2
    systemctl status mongod
}

modify_hosts() {
    echo -e "127.0.0.1\tlocalhost\n131.10.10.aa\tserver.cluster\n131.10.10.bb\tclusterbb\n131.10.10.cc\tclustercc\n131.10.10.dd\tclusterdd\n131.10.10.ee\tclusteree\n131.10.10.ff\tclusterff">/etc/hosts
}

case $1 in
monitor)
    start_monitor;;
zk)
    start_zk;;
kafka)
    start_kafka;;
connect)
    start_connect;;
storm)
    start_storm;;
top)
    start_top;;
mqtt)
    start_mqtt;;
mongodb)
    start_mongodb;;
hosts)
    modify_hosts;;
*)
    echo "usage: mqtt, mongodb, zk, kafka, monitor, connect, storm, top, hosts";;
esac


[](#linux自动打包脚本 "linux自动打包脚本")linux自动打包脚本
-----------------------------------------

> 这是package项目并提取需要JAR到指定目录的自动化打包脚本

### [](#注意事项 "注意事项")注意事项

### [](#shopt-s-extgolb-开启后的效果 "shopt -s extgolb 开启后的效果")shopt -s extgolb 开启后的效果

    开启之后,以下5个模式匹配操作符将被识别:
    ?(pattern-list) - 所给模式匹配0次或1次;
    *(pattern-list) - 所给模式匹配0次以上包括0次;
    +(pattern-list) - 所给模式匹配1次以上包括1次;
    @(pattern-list) - 所给模式仅仅匹配1次;
    !(pattern-list) - 不匹配括号内的所给模式。


### [](#自动打包打包脚本 "自动打包打包脚本")自动打包打包脚本

```s
    #!/bin/bash
    cd /opt/cluster/iotTag/
    echo 解压tag缩包...
    tar -zxvf iot.tar.gz

    echo 更名为iot...
    mv iot.git iot

    echo 打包项目...
    cd opt/cluster/iotTag/iot/
    mvn clean package -Dmaven.test.skip=true

    echo 复制jar到指定目录
    rm -rf jars
    mkdir  jars
    echo 找到并复制jar到指定目录
    find iot -name "iot-*.jar" -exec cp '{}' /opt/cluster/iotTag/jars/ \;
    echo 删除不需要的jar
    cd /opt/cluster/iotTag/jars
    #shopt命令用于显示和设置shell中的行为选项,通过这些选项以增强shell易用性。
    shopt -s extgolb
    shopt -s extglob
    #只保留需要的jar,其他jar删除
    rm -rf !(iot-base-management-rpc-read-1.0.0.jar|iot-base-management-rpc-cud-1.0.0.jar|iot-base-management-rest-api-1.0.0.jar
    |iot-shadow-rest-api-1.0.0.jar|iot-shadow-rpc-cud-1.0.0.jar|iot-shadow-rpc-read-1.0.0.jar
    |iot-app-management-rest-api-1.0.0.jar|iot-app-management-rpc-cud-1.0.0.jar|iot-app-management-rpc-read-1.0.0.jar|iot-app-provider-rest-api-1.0.0.jar
    |iot-status-management-rest-api-1.0.0.jar|iot-status-management-rpc-cud-1.0.0.jar|iot-status-management-rpc-read-1.0.0.jar
    |iot-log-management-rpc-cud-1.0.0.jar|iot-log-management-rpc-read-1.0.0.jar
    |iot-data-statistic-rest-api-1.0.0.jar|iot-data-storm-controller-1.0.0.jar|iot-data-storm-pusher-1.0.0.jar)

总结

linux 自动化脚本可以帮助我们大大减轻部署发布的时间,虽然前期的编写过程比较麻烦,但是用起来却非常方便,试想有很多歌项目环境,如果全部挨个部署岂不是费心又费力呢,所以加入linux脚本阵容是时不我待的.



   Reprint policy


《Linux自动打包脚本》 by jackromer is licensed under a Creative Commons Attribution 4.0 International License
 Previous
JAVA调用HTTPS接口 JAVA调用HTTPS接口
概述 本文主要介绍JAVA实现https接口调用包括忽略本地证书方式和加载本地证书方式两种. 第一种:忽略本地证书方式 忽略本地证书方式就是服务器不对客户端证书做校验的网站比如 www.baidu.com,和其他https网站不一样,其
2019-08-27
Next 
JAVA-FILTER使用 JAVA-FILTER使用
概述 java过滤器是Servlet技术中最实用的技术,Web开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图片文件或静态 html 文件等进行拦截,从而实现一些特殊的功能。 可
2019-08-27
  目录