• Centos7 自部署中间件开机启动,以及java应用开机启动方法


    一、zookeeper

    cd /etc/rc.d/init.d/
    touch zookeeper
    chmod +x zookeeper
    vi zookeeper
    
    #以下为内容,自行修改 路径
    
    #!/bin/bash
    ##chkconfig:2345 10 90
    
    #description:service zookeeper
    #修改为自己的目录
    export     ZOO_LOG_DIR=/data/apache-zookeeper-3.7.0/logs
    ZOOKEEPER_HOME=/data/apache-zookeeper-3.7.0/
    case  "$1"   in
         start)  su  root  ${ZOOKEEPER_HOME}/bin/zkServer.sh  start;;
         start-foreground)  su  root ${ZOOKEEPER_HOME}/bin/zkServer.sh   start-foreground;;
         stop)  su  root  ${ZOOKEEPER_HOME}/bin/zkServer.sh  stop;;
         status)  su root  ${ZOOKEEPER_HOME}/bin/zkServer.sh    status;;
         restart)  su root   ${ZOOKEEPER_HOME}/bin/zkServer.sh   restart;;
         upgrade)su root  ${ZOOKEEPER_HOME}/bin/zkServer.sh  upgrade;;
         print-cmd)su root  ${ZOOKEEPER_HOME}/bin/zkServer.sh  print-cmd;;
         *)  echo "requirestart|start-foreground|stop|status|restart|print-cmd";;
    esac
    
    service zookeeper start/stop
    
    #加入启动,以下二选一执行
    chkconfig --add zookeeper
    chkconfig zookeeper on
    
    #查看启动
    chkconfig --list
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    二、redis

    
    cd /etc/rc.d/init.d/
    vi redis
    
    #以下为内容,自行修改 路径
    
    #!/bin/bash
    #chkconfig: 2345 10 90  
    #description: Start and Stop redis   
    PATH=/usr/local/bin:/sbin:/usr/bin:/bin
    REDISPORT=6379
    EXEC=/data/redis-7.0.4/src/redis-server   #对应你自己的配置地址
    REDIS_CLI=/data/redis-7.0.4/src/redis-cli   #对应你自己的配置地址
    PIDFILE=/var/run/redis.pid
    CONF="/data/redis-7.0.4/redis.conf"  #对应你自己的配置地址
    AUTH="Ecan@8722."
    case "$1" in
         start)
                 if [ -f $PIDFILE ]
                 then
                         echo "$PIDFILE exists, process is already running or crashed."  
                 else
                         echo "Starting Redis server..."  
                         $EXEC $CONF
                 fi
                 if [ "$?"="0" ]
                 then
                         echo "Redis is running..."  
                 fi
                 ;;
         stop)
                 if [ ! -f $PIDFILE ]
                 then
                         echo "$PIDFILE exists, process is not running."  
                 else
                         PID=$(cat $PIDFILE)
                         echo "Stopping..."  
                        $REDIS_CLI -p $REDISPORT  SHUTDOWN
                         sleep 2
                        while [ -x $PIDFILE ]   
                        do
                                 echo "Waiting for Redis to shutdown..."  
                                sleep 1
                         done
                         echo "Redis stopped"  
                 fi
                 ;;
         restart|force-reload)
                 ${0} stop
                 ${0} start
                 ;;
         *)
                echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
                 exit 1
    esac
    
    chmod +x redis
    chkconfig --add redis
    chkconfig redis on
    
    chkconfig --list
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    三、rabbitmq

    systemctl enable rabbitmq-server
    
    • 1

    四、activemq

    cd /etc/rc.d/init.d/
    vi activemq
    
    #!/bin/sh
    #
    # /etc/init.d/activemq
    # chkconfig: 345 63 37
    # description: activemq servlet container.
    # processname: activemq 5.15.2
    
    # Source function library.
    #. /etc/init.d/functions
    # source networking configuration.
    #. /etc/sysconfig/network
    
    export ACTIVEMQ_HOME=/data/apache-activemq-5.15.0
    
    case "$1" in
        start)
            sh $ACTIVEMQ_HOME/bin/activemq start
        ;;
        stop)
            sh $ACTIVEMQ_HOME/bin/activemq stop
        ;;
        status)
            sh $ACTIVEMQ_HOME/bin/activemq status
        ;;
        restart)
            sh $ACTIVEMQ_HOME/bin/activemq stop
            sleep 1
            sh $ACTIVEMQ_HOME/bin/activemq start
        ;;
    
    esac
    exit 0
    
    
    chmod 755 activemq
    #加入启动,以下二选一执行
    chkconfig --add activemq
    chkconfig activemq on
    
    #查看启动
    chkconfig --list
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    五、开机启动java应用

    #进入目录
    cd /usr/lib/systemd/system
    
    #新建服务 news-app 改为自己应用的名字
    vi news-app.service
    
    #以下为内容
    #!/bin/sh
    [Unit]
    Description=news-app #改为你自己的应用名字
    After=syslog.target network.target remote-fs.target nss-lookup.target
    [Service]
    Type=forking
    ExecStart=/data/news-app/startup.sh #改为你自己的启动脚本
    ExecStop=/data/news-app/startup.sh #改为你自己的启动脚本
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target
    
    #文件内容结束,权限授权
    
    chmod +x news-app.service
    #重新加载服务
    systemctl daemon-reload
    #启动服务器
    systemctl start news-app
    #查看服务状态
    systemctl status news-app
    #开启启动服务
    systemctl enable news-app
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    startup.sh 给大家提供一个springboot的启动脚本,以下脚本每次执行会自动关闭程序并启动程序,避免手动关闭应用的麻烦。
    请自行修改所有 news-app 的地方,java 附带参数自行修改,这里涵盖了dubbo。

    #!/bin/bash
    basepath=$(cd `dirname $0`; pwd)
    arr=`ps -ef | grep news-app-v1.0.0 | grep -v grep | awk '{print $2}'`
    echo get pid: ${arr}
    for i in ${arr[@]};do
    echo kill pid ${i}...
    kill -9 ${i}
    done;
    echo kill finished!
    cd ${basepath}
    echo start application
    nohup java -Xmx250M -Xms250M -Ddubbo.network.interface.preferred=eth0 -Ddubbo.provider.telnet=cd,clear,count,pwd,exit,invoke,log,ps,select,shutdown,status,trace,help,ls -jar -Dfile.encoding=UTF-8 -Dloader.path=${basepath}/lib,${basepath}/config  ${basepath}/news-app-v1.0.0.jar.original >/dev/null 2>&1 &
    echo start end...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    迁移学习和域自适应
    Java复习第二弹!
    入门案例:Hello World
    验证码识别全流程实战
    python开发之个微群聊机器人的开发
    MySQL高级SQL语句
    【C++】类与对象 第二篇(构造函数,析构函数,拷贝构造,赋值重载)
    blender快捷键(持续更新)
    MySQL根据备注查询表、字段
    数据结构与算法设计分析—— 数据结构及常用算法
  • 原文地址:https://blog.csdn.net/kkillala/article/details/133132837