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

一、zookeeper

bash 复制代码
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

二、redis

bash 复制代码
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

三、rabbitmq

sql 复制代码
systemctl enable rabbitmq-server

四、activemq

bash 复制代码
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

五、开机启动java应用

bash 复制代码
#进入目录
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

startup.sh 给大家提供一个springboot的启动脚本,以下脚本每次执行会自动关闭程序并启动程序,避免手动关闭应用的麻烦。

请自行修改所有 news-app 的地方,java 附带参数自行修改,这里涵盖了dubbo。

bash 复制代码
#!/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...
相关推荐
麦聪聊数据9 分钟前
企业数据市场建设(四):流程闭环与价值运营,让数据市场真正转起来
运维·数据库
是潮汕的灿灿展吖34 分钟前
Centos离线部署nfs操作
linux·运维·centos
FII工业富联科技服务44 分钟前
从灯塔工厂到AI Factory新模式:AI正在重构制造业的四大核心能力
大数据·运维·人工智能·重构·ar·制造
小五传输1 小时前
自主可控建设指南:Serv-u替代方案,实现平滑迁移业务不中断
大数据·运维·安全
jun_bai1 小时前
Orthanc服务器使用java上传dicom影像文件
java·运维·服务器
BullSmall1 小时前
Anolis OS 8.10 Docker 部署 SonarQube 9.9 完整教程
运维·docker·容器
AAA@峥1 小时前
CentOS7 搭建 ELK 企业级日志集群:从部署到日志可视化完整实战
运维·elk·centos
ZKNOW甄知科技1 小时前
燕千云深度集成飞书:以AI之力,开启无感IT运维体验
大数据·运维·网络·数据库·人工智能·低代码·集成学习
叮咚侠1 小时前
docker安装的kibana+elasticsearch,突然kibana界面打不开了
运维·jenkins
nVisual1 小时前
数据中心机柜负载均衡与三相相位监控方案
运维·网络·负载均衡·数据中心布线·综合布线管理软件