侧边栏壁纸
博主头像
★街角晚灯★ 博主等级

博观而约取 厚积而薄发

  • 累计撰写 468 篇文章
  • 累计创建 185 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

nohup部署更新升级Java后端项目

WinJay
2024-09-27 / 0 评论 / 0 点赞 / 37 阅读 / 0 字 / 正在检测是否收录...

Nohup 部署更新升级Java后端项目

#!/bin/bash
# Author:WinJayX
# date:2024-05-11
# Maintainer WinJayX
# func:更新升级Java后端项目
#!/bin/sh

# JAR文件的名称和路径
JAR_NAME="lit-message.jar"
JAR_PATH="/tmp/$JAR_NAME" # JAR文件的完整路径
TARGET_DIR="/tmp/" # 指定要删除JAR文件的目录
PID_FILE="nohup.pid" # 保存PID的文件
LOG_FILE="LitMessage.log" # 日志文件


# 重新启动nohup服务
# 首先,尝试杀死旧的进程
if [ -f "/mnt/000.Docker/003.Lit-Message/$PID_FILE" ]; then
    read -r pid < "/mnt/000.Docker/003.Lit-Message/$PID_FILE"
    if kill -0 "$pid" 2>/dev/null; then
        kill "$pid"
        echo "Old process with PID $pid terminated."
    else
        echo "No process found with PID $pid."
    fi
    rm -f "/mnt/000.Docker/003.Lit-Message/$PID_FILE" # 清理PID文件
fi

rm -f "/mnt/000.Docker/003.Lit-Message/$JAR_NAME"
cp "$JAR_PATH" "/mnt/000.Docker/003.Lit-Message/"
# 再次启动新的JAR文件
# nohup java -jar "$JAR_NAME" > "$LOG_FILE" 2>&1 &
nohup java -jar "/mnt/000.Docker/003.Lit-Message/$JAR_NAME"  --spring.active=dev > "/mnt/000.Docker/003.Lit-Message/$LOG_FILE" 2>&1 &
echo $! > "/mnt/000.Docker/003.Lit-Message/$PID_FILE"

# rm -f "$TARGET_DIR"/"$JAR_NAME"
echo "New process started with PID $!"

# 脚本结束
#!/bin/bash

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# 项目名称
SERVER_NAME="manager"

# jar名称
JAR_NAME="argusdbm.jar"

# 进入bin目录
cd `dirname $0`
# bin目录绝对路径
BIN_DIR=`pwd`
# 返回到上一级项目根目录路径
cd ..
# 打印项目根目录绝对路径
# `pwd` 执行系统命令并获得结果
DEPLOY_DIR=`pwd`

# 外部配置文件绝对目录,如果是目录需要/结尾,也可以直接指定文件
# 如果指定的是目录,spring则会读取目录中的所有配置文件
CONF_DIR=$DEPLOY_DIR/config
# 应用的端口号
SERVER_PORT=1159

PIDS=`ps -ef | grep java | grep "$CONF_DIR" | awk '{print $2}'`
if [ "$1" = "status" ]; then
    if [ -n "$PIDS" ]; then
        echo "The ArgusDBM $SERVER_NAME is running...!"
        echo "PID: $PIDS"
        exit 0
    else
        echo "The ArgusDBM $SERVER_NAME is stopped"
        exit 0
    fi
fi

if [ -n "$PIDS" ]; then
    echo "ERROR: The ArgusDBM $SERVER_NAME already started!"
    echo "PID: $PIDS"
    exit 1
fi

if [ -n "$SERVER_PORT" ]; then
    # linux 下查询端口是否占用
    SERVER_PORT_COUNT=`netstat -tln | grep :$SERVER_PORT | wc -l`
    if [ $SERVER_PORT_COUNT -gt 0 ]; then
        echo "ERROR: netstat the ArgusDBM $SERVER_NAME port $SERVER_PORT already used!"
        exit 1
    fi
    # mac 下查询端口是否占用
    LSOF_AVA=`command -v lsof | wc -l`
    if [ $LSOF_AVA -gt 0 ]; then
        SERVER_PORT_COUNT=`lsof -i:$SERVER_PORT | grep java | wc -l`
        if [ $SERVER_PORT_COUNT -gt 0 ]; then
            echo "ERROR: lsof the ArgusDBM $SERVER_NAME port $SERVER_PORT already used!"
            exit 1
        fi
    fi
fi

# 项目日志输出绝对路径
LOGS_DIR=$DEPLOY_DIR/logs
# 如果logs文件夹不存在,则创建文件夹
if [ ! -d $LOGS_DIR ]; then
    mkdir $LOGS_DIR
fi

# JVM Configuration
JAVA_OPTS=" -Duser.timezone=Asia/Shanghai"

JAVA_MEM_OPTS=" -server -XX:SurvivorRatio=6 -XX:+UseParallelGC "

# 加载外部log文件的配置
LOG_IMPL_FILE=logback-spring.xml
LOGGING_CONFIG=""
if [ -f "$CONF_DIR/$LOG_IMPL_FILE" ]
then
    LOGGING_CONFIG="-Dlogging.config=$CONF_DIR/$LOG_IMPL_FILE"
fi
CONFIG_FILES=" -Dlogging.path=$LOGS_DIR $LOGGING_CONFIG -Dspring.config.location=$CONF_DIR/ "
echo -e "You can review logs at ArgusDBM/logs"
echo -e "Starting the ArgusDBM $SERVER_NAME ..."
nohup /opt/xc/jdk11/jdk-11.0.1/bin/java $JAVA_OPTS $JAVA_MEM_OPTS $CONFIG_FILES -jar $DEPLOY_DIR/$JAR_NAME >logs/startup.log 2>&1 &

COUNT=0
while [ $COUNT -lt 1 ]; do
    echo "... "
    sleep 1
    if [ -n "$SERVER_PORT" ]; then
        COUNT=`netstat -an | grep $SERVER_PORT | wc -l`
    else
       COUNT=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}' | wc -l`
    fi
    if [ $COUNT -gt 0 ]; then
        break
    fi
done

echo "Service Start Success!"
PIDS=`ps -f | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`
echo "Service PID: $PIDS"
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区