在 Linux 系统中启动 Spring Boot 项目,通常可以通过编写一个简单的 shell 脚本来实现。以下是一个基本的示例:

#!/bin/bash
# 定义应用名称
APP_NAME=spring-boot-app.jar

# 启动方法
start(){
  # 判断应用是否已启动
  P_ID=$(ps -ef | grep $APP_NAME | grep -v grep | grep -v kill | awk '{print $2}')
  if [ "$P_ID" == "" ]; then
    echo "=== start $APP_NAME"
    nohup java -jar $APP_NAME > /dev/null 2>&1 &
    echo "=== $APP_NAME started"
  else
    echo "=== $APP_NAME is already running PID=$P_ID"
  fi
}

# 停止方法
stop(){
  P_ID=$(ps -ef | grep $APP_NAME | grep -v grep | grep -v kill | awk '{print $2}')
  if [ "$P_ID" == "" ]; then
    echo "=== $APP_NAME is not running"
  else
    echo "=== stop $APP_NAME"
    kill $P_ID
    echo "=== $APP_NAME stopped"
  fi
}

# 输出运行状态
status(){
  P_ID=$(ps -ef | grep $APP_NAME | grep -v grep | grep -v kill | awk '{print $2}')
  if [ "$P_ID" == "" ]; then
    echo "=== $APP_NAME is not running"
  else
    echo "=== $APP_NAME is running PID=$P_ID"
  fi
}

# 重启方法
restart(){
  stop
  sleep 2
  start
}

# 根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
  "start")
    start
  ;;
  "stop")
    stop
  ;;
  "status")
    status
  ;;
  "restart")
    restart
  ;;
  *)
    echo "Usage: {start|stop|status|restart}"
    exit 1
  ;;
esac

这个脚本定义了 start、stop、status 和 restart 四个方法,分别用于启动、停止、查看状态和重启 Spring Boot 应用。你可以将这个脚本保存为一个 .sh 文件,如 app.sh。然后通过运行 chmod +x app.sh 命令来为脚本添加执行权限。

使用方法如下:

  • 启动应用:./app.sh start
  • 停止应用:./app.sh stop
  • 查看应用状态:./app.sh status
  • 重启应用:./app.sh restart

这个脚本的工作原理是通过 ps -ef 命令和 grep 命令来查找运行中的应用进程,然后通过 kill 命令来停止进程。nohup& 用于在后台运行应用并忽略挂起信号。

请注意,这只是一个基本的示例,实际使用时可能需要根据你的具体需求进行修改。例如,你可能需要指定 JVM 参数,或者使用特定的 Spring Boot 配置文件等。


海外免备案云服务器链接:www.tsyvps.com

蓝易云香港五网CN2 GIA/GT精品网络服务器。拒绝绕路,拒绝不稳定。

蓝易云是一家专注于香港及国内数据中心服务的提供商,提供高质量的服务器租用和云计算服务、包括免备案香港服务器、香港CN2、美国服务器、海外高防服务器、国内高防服务器、香港VPS等。致力于为用户提供稳定,快速的网络连接和优质的客户体验。
最后修改:2023 年 09 月 08 日
如果觉得我的文章对你有用,请随意赞赏