最近开发一个新的系统,由于没有买相关的部署服务,所以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 这两个脚本是否正常,最后大功告成。
觉得写的不错的朋友,请点点赞!万分感谢