博客
关于我
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/

    你可能感兴趣的文章
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>
    SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
    查看>>
    ORM sqlachemy学习
    查看>>
    Ormlite数据库
    查看>>
    orm总结
    查看>>
    os.path.join、dirname、splitext、split、makedirs、getcwd、listdir、sep等的用法
    查看>>
    os.system 在 Python 中不起作用
    查看>>
    OSCACHE介绍
    查看>>
    SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
    查看>>
    OSChina 周五乱弹 ——吹牛扯淡的耽误你们学习进步了
    查看>>
    OSChina 周四乱弹 ——程序员为啥要买苹果手机啊?
    查看>>
    OSError: no library called “cairo-2“ was foundno library called “cairo“ was foundno library called
    查看>>
    Osgi环境配置
    查看>>