适用于 Java项目开发,打包为jar的应用程序部署,操作系统为CentOS 7.9。
安装Java运行时,此处省略800字…
将打包好的*.jar文件、config文件上传到服务器/home/8001目录下。
cd /home/8001
vim run.sh
输入以下内容:
#!/bin/bash
# 修改为jar包名称
AppName=GovServices.jar
# 修改端口号
PortID=8001
# 以下无需修改
# 设置Java虚拟机系统内存参数
JVM_OPTS="-server -Xms256M -Xmx256M -XX:PermSize=128M -XX:MaxPermSize=512M"
PID=""
getpid() {
TempPID=$(netstat -antlp | grep $PortID | awk '{print $7}')
echo ${TempPID%%/java}
}
function start() {
PID=$(getpid)
if [ x"$PID" != x"" ]; then
echo "$AppName (pid:$PID) is running..."
else
echo "Start $AppName..."
nohup java -jar $JVM_OPTS $AppName >/dev/null 2>&1 &
sleep 5
local t=1
while [ x"$PID" == x"" ] && [ $t -lt 20 ]; do
PID=$(getpid)
if [ x"$PID" != x"" ]; then
echo "Start $AppName (pid:$PID) success..."
else
echo "$((t*5)) seconds..."
sleep 5
fi
(( t+=1 ))
done
if [ x"$PID" == x"" ]; then
echo "Failed to start $AppName. See \"cat logs/sys-info.log\" and \"cat logs/sys-error.log\" for details."
fi
fi
}
function stop() {
PID=$(getpid)
if [ x"$PID" != x"" ]; then
echo "Stop $AppName (pid:$PID)"
kill -TERM $PID
echo "$AppName (pid:$PID) exiting..."
while [ x"$PID" != x"" ]; do
sleep 3
PID=$(getpid)
done
echo "$AppName exited."
else
echo "$AppName already stopped."
fi
}
function restart() {
stop
sleep 5
start
}
function status() {
PID=$(getpid)
if [ x"$PID" != x"" ]; then
echo "$AppName (pid:$PID) is running..."
else
echo "$AppName is not running..."
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*) ;;
esac
:wq命令保存退出后,chmod u+x run.sh
授予当前用户执行权限。
服务控制命令:
# 启动
./run.sh start
# 停止
./run.sh stop
# 重启
./run.sh restart
# 查看状态
./run.sh status