jar程序包,脚本配置文件(***.service)

本文详细描述了在没有购买部署服务的情况下,开发人员如何在Linux上自行搭建和部署JAR应用,包括start.sh、reload.sh和shutdown.sh脚本的编写,以及使用systemd的gateway_iot.service配置文件管理和启动/重启操作的流程。
摘要由CSDN通过智能技术生成

最近开发一个新的系统,由于没有买相关的部署服务,所以linux上的启动服务和配置都是由开发人员自己搭建并部署。

一:JAR的部署路径

 二:**.sh shell脚本的部署路径和代码如下

 2.1 start.sh 启动脚本代码如下

#!/bin/sh
#Server run parameters
PORT=8081
JAR_PATH=/home/vision/release/gateway
JAR_NAME=vision-gateway.jar
IP_ADDRESS=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

tpid=`ps -ef|grep vision-gateway|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'The server is running. Please stop the server first!'
else
    echo 'Start the server...'
    if [ -f "/home/vision/release/gateway/tpid" ]; then
        rm -f /home/vision/script/gateway/iot.pid
    fi
    nohup java -jar ${JAR_PATH}/${JAR_NAME} --IP_ADDRESS=${IP_ADDRESS} --PORT=${PORT} &
    echo $! > /home/vision/script/gateway/iot.pid
    echo 'The server started success!'
fi

2.2 reload.sh 重启脚本代码如下

#!/bin/sh
#Server run parameters
PORT=8081
JAR_PATH=/home/vision/release/gateway
JAR_NAME=vision-gateway.jar
IP_ADDRESS=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

tpid=`ps -ef|grep vision-gateway|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    kill -15 $tpid
fi
sleep 5
tpid=`ps -ef|grep vision-gateway|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    kill -9 $tpid
fi

tpid=`ps -ef|grep vision-gateway|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'Fail to stop the server.'
    echo 'Fail to restart the server!'
else
    echo 'The server has stopped.' 
    echo 'Start the server...'
    rm -f /home/vision/script/gateway/iot.pid
    nohup java -jar ${JAR_PATH}/${JAR_NAME} --IP_ADDRESS=${IP_ADDRESS} --PORT=${PORT} &
    echo $! > /home/vision/script/gateway/iot.pid
    echo 'The server restarted success!'
fi

2.3 shutdown.sh 关闭程序脚本代码如下

#!/bin/sh
#Server run parameters
PORT=8081
JAR_PATH=/home/vision/release/gateway
JAR_NAME=vision-gateway.jar
IP_ADDRESS=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

tpid=`ps -ef|grep vision-gateway|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    kill -15 $tpid
fi
sleep 5
tpid=`ps -ef|grep vision-gateway|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    kill -9 $tpid
fi

tpid=`ps -ef|grep vision-gateway|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
    echo 'Fail to stop the server.'
    echo 'Fail to restart the server!'
else
    echo 'The server has stopped.' 
    echo 'Start the server...'
    rm -f /home/vision/script/gateway/iot.pid
    nohup java -jar ${JAR_PATH}/${JAR_NAME} --IP_ADDRESS=${IP_ADDRESS} --PORT=${PORT} &
    echo $! > /home/vision/script/gateway/iot.pid
    echo 'The server restarted success!'
fi

三:配置脚本配置文件(***.service)

进入路径:/usr/lib/systemd/system

执行 vim gateway_iot.service 并输入以下代码

[Unit]

Description=gateway_iot.service

After=network.target

[Service]

Type=forking

ExecStart=/home/vision/script/gateway/start.sh

ExecReload=/home/vision/script/gateway/reload.sh

ExecStop=/home/vision/script/gateway/shutdown.sh

PrivateTmp=true

[Install]

WantedBy=multi-user.target

最后 重启systemctl配置:systemctl daemon-reload。

四:执行启动命令,检查是否正确

systemctl start gateway_iot.service

检查 ps -ef|grep gateway,当出现以下代码,说明执行成功,可以测试下,systemctl stop gateway_iot.service,systemctl restart gateway_iot.service 这两个脚本是否正常,最后大功告成。

 觉得写的不错的朋友,请点点赞!万分感谢 

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值