博客
关于我
Java-springboot部署脚本
阅读量:158 次
发布时间:2019-02-28

本文共 1333 字,大约阅读时间需要 4 分钟。

Bash脚本驱动JAR程序管理

项目部署说明

将项目文件和脚本置于同一工作目录中即可运行。脚本仅需修改第一个变量值即可适配不同项目配置。

脚本功能说明

1. 参数说明

./app.sh {start|stop|restart|status}
  • start:启动JAR程序
  • stop:立即停止运行的JAR程序
  • restart:重启已停止的JAR程序
  • status:显示JAR程序当前状态

2. 功能实现细节

1. 检查程序状态

is_exist() {    local PID=$(ps -ef | grep ${APP_NAME} | grep -v grep | awk '{print $2}')    [[ -z "${PID}" ]] && return 1 || return 0}
  • ps -ef 检查系统进程列表
  • grep ${APP_NAME} 搜索特定JAR文件名
  • grep -v grep 排除grep自身进程
  • awk '{print $2}' 提取进程PID
  • 返回状态:存在返回0,不存在返回1

2. 启动程序

start() {    is_exist    if [ $? -eq 0 ]; then        echo "${APP_NAME} is already running. Pid: ${PID}"    else        nohup java -jar ${APP_NAME} > /dev/null 2>&1 &    fi}
  • nohup防止控制终端关闭时程序退出
  • > /dev/null 2>&1 将输出和错误日志隐藏
  • 使用&后台运行程序

3. 停止程序

stop() {    is_exist    if [ $? -eq 0 ]; then        kill -9 ${PID}    else        echo "${APP_NAME} is not running"    fi}
  • 使用kill -9强制终止进程
  • 无法停止时提示用户程序未运行

4. 查看状态

status() {    is_exist    if [ $? -eq 0 ]; then        echo "${APP_NAME} is running. Pid: ${PID}"    else        echo "${APP_NAME} is NOT running."    fi}
  • 显示程序运行状态和对应PID
  • 状态不正常时提示用户

5. 重启程序

restart() {    stop    start}
  • 先停止程序,再启动新的进程

3. 使用示例

# 启动程序./app.sh start# 停止程序./app.sh stop# 重启程序./app.sh restart# 查看状态./app.sh status

注意事项

  • 确保脚本和JAR程序位于同一目录
  • 修改APP_NAME变量为实际JAR文件名
  • 在不同Linux系统中可能需要调整命令
  • 使用-9选项时,程序可能无法保存状态数据
  • 部分环境可能需要使用sudo执行脚本
  • 通过以上命令,您可以轻松管理JAR程序的运行状态,确保系统稳定性和可用性。

    转载地址:http://csnc.baihongyu.com/

    你可能感兴趣的文章
    python win32api键盘_Python win32api.keybd_event模拟键盘输入
    查看>>
    python Windows显示当前鼠标坐标
    查看>>
    python Workbook 表格宽度
    查看>>
    python write报错a byte-like object is required.not str
    查看>>
    PYTHON调用大模型实现图片生成
    查看>>
    python yaml.dump 缩进错误
    查看>>
    Python yield generator
    查看>>
    Python yield 使用浅析
    查看>>
    Python yield解析:深入理解生成器的魔力
    查看>>
    Python You are using pip version 10.0.1, however version 19.3.1 is available.
    查看>>
    python zipfile模块学习笔记(一)
    查看>>
    Python zip函数 详解(全)
    查看>>
    Python \r\n与\n的转换
    查看>>
    python __new__中单例的作用
    查看>>
    python | aiofiles,一个超酷的 Python 库!
    查看>>
    python | akshare,一个超强的 开源Python 金融数据接口库!
    查看>>
    python | alabaster,一个强大的 关于alabaster 主题 Python 库!
    查看>>
    python | algorithms,一个超赞的 集合常用算法的Python 库!
    查看>>
    python调用jpype 报错:OSError JVM is already started和JVM cannot be restarted
    查看>>
    python | authlib,一个强大的 Python 库!
    查看>>