springboot项目启动、停止脚本
时间: 2023-09-28 10:03:21 浏览: 121
可以使用以下脚本来启动和停止 Spring Boot 项目:
启动脚本:
```bash
#!/bin/bash
nohup java -jar /path/to/your/spring-boot-project.jar > /dev/null 2>&1 &
echo $! > /path/to/your/pid.file
```
这个脚本会以后台进程的方式启动 Spring Boot 项目,并将进程 ID 写入到 pid.file 文件中。
停止脚本:
```bash
#!/bin/bash
kill $(cat /path/to/your/pid.file)
```
这个脚本会读取 pid.file 文件中的进程 ID,并向该进程发送 TERM 信号来停止 Spring Boot 项目。
请注意,上述脚本中的路径和文件名需要根据你的实际情况进行修改。另外,在使用这些脚本之前,你需要确保已经安装了 Java 运行环境。
相关问题
springboot项目启动脚本怎么写
在Spring Boot项目中,可以使用脚本来启动应用程序。脚本文件可以是任何可执行文件,例如Shell脚本或Windows批处理文件。下面是一个简单的启动脚本示例,可以将其用作参考:
```bash
#!/bin/bash
# 定义变量
APP_NAME=myapp
APP_PORT=8080
# 停止应用程序
PID=$(ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}')
if [ -n "$PID" ]
then
echo "Stopping $APP_NAME"
kill $PID
fi
# 启动应用程序
echo "Starting $APP_NAME"
nohup java -jar $APP_NAME.jar --server.port=$APP_PORT > /dev/null 2>&1 &
```
解释一下上面的脚本:
- 第一行`#!/bin/bash`表示使用Bash脚本语言。
- 第二行定义了应用程序的名称和端口号。
- 第四行会检查应用程序是否已经运行,并停止它。
- 最后一行启动应用程序,并将输出重定向到/dev/null,表示不输出任何信息。
上述脚本中的`nohup`命令可以使应用程序在后台运行,并且即使用户退出当前终端,应用程序也会继续运行。
使用该脚本,只需将其保存为可执行文件,然后运行即可启动Spring Boot应用程序。
java运维shell脚本一般怎么写,请举一个 springboot项目的 运维脚本示例
Java运维shell脚本一般使用Bash或者其他Shell语言来编写,主要是为了方便运维人员进行一些自动化操作,比如部署、备份、启动和停止等。下面给出一个Spring Boot项目的运维脚本示例:
```bash
#!/bin/bash
# 定义一些变量
APP_NAME="my-application"
APP_HOME="/opt/${APP_NAME}"
APP_JAR="${APP_HOME}/${APP_NAME}.jar"
PID_FILE="${APP_HOME}/${APP_NAME}.pid"
LOG_FILE="${APP_HOME}/${APP_NAME}.log"
# 检查应用是否已经启动,如果已经启动则退出
if [ -f "${PID_FILE}" ]; then
pid=$(cat "${PID_FILE}")
if ps -p "${pid}" > /dev/null; then
echo "${APP_NAME} is already running with PID ${pid}."
exit 1;
fi
fi
# 启动应用
nohup java -jar "${APP_JAR}" > "${LOG_FILE}" 2>&1 &
pid=$!
echo "${pid}" > "${PID_FILE}"
echo "Started ${APP_NAME} with PID ${pid}."
# 停止应用
function stop() {
if [ -f "${PID_FILE}" ]; then
pid=$(cat "${PID_FILE}")
kill -TERM "${pid}"
rm "${PID_FILE}"
echo "Stopped ${APP_NAME} with PID ${pid}."
else
echo "${APP_NAME} is not running."
fi
}
# 其他操作,比如备份、部署等
function backup() {
# TODO: 备份操作
}
function deploy() {
# TODO: 部署操作
}
# 根据传入的参数执行不同的操作
case "$1" in
start)
# 启动应用
;;
stop)
# 停止应用
stop
;;
restart)
# 重启应用
stop
sleep 5
start
;;
backup)
# 备份应用
backup
;;
deploy)
# 部署应用
deploy
;;
*)
echo "Usage: $0 {start|stop|restart|backup|deploy}"
exit 1
esac
```
这个脚本定义了一些变量,比如应用名称、应用目录、应用Jar包、PID文件和日志文件等。然后定义了一个`stop`函数用来停止应用,以及其他操作如备份和部署等。最后使用`case`语句来根据传入的参数执行不同的操作。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![sh](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![sh](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![sh](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)