若依jar包运行脚本,从零到一:用Bash脚本实现JAR应用的启动、停止与监控

脚本使用说明

  1. 启动应用sh app.sh start
  2. 停止应用sh app.sh stop
  3. 检查应用状态sh app.sh status
  4. 重启应用sh app.sh restart

注意事项

  • 请确保你的系统上安装了 Java 环境,并且 ruoyi-admin.jar 文件存在于脚本可以访问的路径中。

  • 脚本中使用了 lsof 命令来检查端口占用情况,但这是一个拼写错误。通常,我们使用 lsofnetstatss 命令来实现这个功能。你需要根据系统环境替换正确的命令。

  • 脚本中的 nohup 也是拼写错误,正确的应该是 nohup

  • nobup.log 文件用于记录应用启动时的输出,请确保脚本对该文件有写入权限。

    #!/bin/bash
    APP_NAME=ruoyi-admin.jar
    PORT=9021 # 假设你的应用绑定到这个端口

    usage() {
    echo "Usage: sh app.sh [start|stop|restart|status]"
    exit 1
    }

    is_exist(){
    pid=(lsof -t -i :PORT) # 使用lsof命令查找使用指定端口的PID
    if [ -z "$pid" ]; then
    return 1
    else
    return 0
    fi
    }

    start(){
    is_exist
    if [ ? -eq 0 ]; then echo "{APP_NAME} is already running on port PORT." else # nohup java -Xms256m -Xmx512m -jar APP_NAME > /dev/null 2>&1 &
    nohup java -Xms256m -Xmx512m -jar $APP_NAME >nobup.log 2>&1 &
    fi
    }

    stop(){
    is_exist
    if [ ? -eq 0 ]; then kill -9 pid # 关闭找到的进程
    echo "Stopped {APP_NAME} running on port PORT."
    else
    echo "{APP_NAME} is not running on port PORT."
    fi
    }

    status(){
    is_exist
    if [ ? -eq 0 ]; then echo "{APP_NAME} is running on port PORT. Pid is pid"
    else
    echo "{APP_NAME} is NOT running on port PORT."
    fi
    }

    restart(){
    stop
    start
    }

    case "$1" in
    "start")
    start
    ;;
    "stop")
    stop
    ;;
    "status")
    status
    ;;
    "restart")
    restart
    ;;
    *)
    usage
    ;;
    esac

AI注释版

复制代码
#!/bin/bash  
# 声明应用的名称和使用的端口号  
APP_NAME=ruoyi-admin.jar  
PORT=9021  # 假设应用绑定到这个端口  
  
# 使用函数来显示脚本的使用方式,并退出脚本  
usage() {  
    echo "Usage: sh app.sh [start|stop|restart|status]"  
    exit 1  
}  
  
# 检查应用是否已经在运行  
is_exist(){  
    # 使用 lsof 命令查找使用指定端口的进程ID,这里应该是 lsof 的拼写错误,应为 lsof 或 lsof 的替代品如 netstat、ss  
    pid=$(lsof -t -i :$PORT)  
    if [ -z "$pid" ]; then  
        # 如果 pid 为空,表示没有找到对应的进程,返回 1  
        return 1  
    else  
        # 如果找到了进程,返回 0  
        return 0  
    fi  
}  
  
# 启动应用的函数  
start(){  
    # 检查应用是否已经在运行  
    is_exist  
    if [ $? -eq 0 ]; then  
        # 如果应用已经在运行,则输出提示信息  
        echo "${APP_NAME} is already running on port $PORT."  
    else  
        # 如果应用没有运行,则启动应用,并将输出重定向到 nobup.log 文件  
        # 注意:nohup 应该是 nohup 的拼写错误  
        nohup java -Xms256m -Xmx512m -jar $APP_NAME >nobup.log 2>&1 &  
    fi  
}  
  
# 停止应用的函数  
stop(){  
    # 检查应用是否在运行  
    is_exist  
    if [ $? -eq 0 ]; then  
        # 如果应用在运行,则强制终止该进程  
        kill -9 $pid  
        # 输出停止应用的提示信息  
        echo "Stopped ${APP_NAME} running on port $PORT."  
    else  
        # 如果应用没有运行,则输出提示信息  
        echo "${APP_NAME} is not running on port $PORT."  
    fi  
}  
  
# 检查应用状态的函数  
status(){  
    # 检查应用是否在运行  
    is_exist  
    if [ $? -eq 0 ]; then  
        # 如果应用在运行,则输出应用的运行状态和进程ID  
        echo "${APP_NAME} is running on port $PORT. Pid is $pid"  
    else  
        # 如果应用没有运行,则输出提示信息  
        echo "${APP_NAME} is NOT running on port $PORT."  
    fi  
}  
  
# 重启应用的函数  
restart(){  
    # 停止应用  
    stop  
    # 启动应用  
    start  
}  
  
# 根据传入的参数执行相应的操作  
case "$1" in  
    "start")  
        start  # 启动应用  
        ;;  
    "stop")  
        stop  # 停止应用  
        ;;  
    "status")  
        status  # 检查应用状态  
        ;;  
    "restart")  
        restart  # 重启应用  
        ;;  
    *)  
        usage  # 如果参数不正确,则显示使用方式  
        ;;  
esac
相关推荐
z落落6 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
CTA量化套保7 小时前
期货量化程序 time.sleep 卡死:天勤单线程与 deadline 替代
python·区块链
allway27 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash
weixin_462446237 小时前
手把手教你用 Bash 脚本自动更新 /etc/hosts —— 自动绑定网卡 IP 与节点名
开发语言·tcp/ip·bash
一个梦醒了7 小时前
安装git bash选项推荐
开发语言·git·bash
谢平康7 小时前
解决用 rm 报bash: /usr/bin/rm: Argument list too long错
linux·运维·运维开发
GIS数据转换器7 小时前
城市排水生命线安全运行监测平台深度解析
java·运维·人工智能·python·安全·数据挖掘·无人机
ct9787 小时前
React 状态管理方案深度对比
开发语言·前端·react
贤哥哥yyds7 小时前
GBK转UTF\-8编码自动转换工具 使用文档
python
数量技术宅7 小时前
2026量化前沿:从Reddit热帖到Python实战,如何用赫斯特指数(Hurst)狙击虚假突破?
开发语言·python