SpringBoot程序的shell控制脚本(startup.sh)

本文介绍了一个用于控制SpringBoot应用(jar包)启动、停止及状态检查的Shell脚本。该脚本适用于单一jar文件环境,并提供了一系列实用功能,如启动、停止、检查应用状态等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用于springboot应用(jar)启、停、状态控制,

一、 SpringBoot程序的shell控制脚本

注意:当前目录中仅有一个jar文件和一个 application-prod.yml 配置文件,可根据您的实际情况做修改。

#!/bin/sh
#########################################################
# Desc: 用于springboot应用(jar)启、停、状态控制,
#      注意:当前目录中仅有一个jar文件和一个 application-prod.yml 配置文件
# Vers: 1.0
# Date: 2022-09-20
# Author: xyg
# Email: 1031867856@qq.com
#########################################################

# 启动
function start() {
	if [ ${tpid} ];  then
		echo -e "Application $APP_NAME is already \033[32mRUNNING\033[0m : $tpid ,no need to start..."
	else
	    nohup java -jar -Dspring.profiles.active=prod ${APP_NAME} > /dev/null 2>&1 &  
		tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
	    check
	fi  
}
# 停止
function stop() {
	if [ ${tpid} ]; then
		echo "Kill process: $tpid, wait a few seconds..."
		kill -15 $tpid
		sleep 5
		tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`

		if [ ${tpid} ]; then
			echo "Application $APP_NAME still running, force kill process!"
			kill -9 $tpid
			sleep 5
			tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
			if [ ${tpid} ]; then
				echo "Application $APP_NAME still running, stop failed!"
			else
				echo 'OK!'
			fi
		else
		    echo 'OK!'
		fi
	else
	    echo -e "Application $APP_NAME is already \033[31mSTOPED\033[0m!"
	fi
}

# 查看运行状态
function check() {
	if [ ${tpid} ];  then
		echo -e "Application $APP_NAME is \033[32mRUNNING\033[0m..."
	else
		echo -e "Application $APP_NAME is \033[31mSTOPED\033[0m!"
	fi
}

# 强制杀进程
function forcekill() {
	if [ ${tpid} ]; then
		echo "Force kill process: $tpid , wait a few seconds..."
		kill -9 $tpid
		sleep 5
	    tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
		if [ ${tpid} ]; then
			echo "Application $APP_NAME still running, stop failed!"
		else
			echo "$APP_NAME force killed success!"
		fi
	else
		echo -e "Application $APP_NAME is already \033[31mSTOPED\033[0m!"
	fi
}

# 读取命令
command=$1

# 进入目录
BASE_PATH=$(cd `dirname $0`;pwd)
cd $BASE_PATH
echo -e ""

# 遍历目录中 jar 文件
count=0
for file in $(ls *.jar) 
do
	eval APP_NAME="$file"
	count=`expr $count + 1 `
done

# 如果含有多个 jar 文件,则提示
if [ ${count} -ne 1 ]; then
	echo -e "One and only one *.jar file is allowed in directory: $BASE_PATH"
	echo -e "Please remove other jar files or versions, then try again..."
else
	if [ ${APP_NAME} ]; then
		# 显示当前目录和对应的 jar 文件
		echo -e "\033[44;30mDIR:\033[0m $BASE_PATH"
		echo -e "\033[45;30mAPP:\033[0m $APP_NAME"
		tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`

		# 指令遍历控制
		if [ "${command}" ==  "start" ]; then
			start

		elif [ "${command}" ==  "stop" ]; then
			stop

		elif [ "${command}" ==  "check" ]; then
			check
			
		elif [ "${command}" ==  "stat" ]; then
			check

		elif [ "${command}" ==  "kill" ]; then
			forcekill

		else
			echo "---------------- HELP ---------------"
			echo "Here is optional command bellow:"
			echo "    start: to start the application."
			echo "    stop:  to stop the application"
			echo "    check: to check the status of application, print 'RUNNING' if it's running."
			echo "    stat:  the same as command 'check'."
			echo "    kill:  to force kill application precess backend."
			echo "    help:  print help info."
			echo "-------------------------------------"
		fi
	else
		echo "Error, no *.jar found!"
	fi
fi
echo -e ""

二、使用效果

1. 直接运行:./startup.sh

在这里插入图片描述

2. 启动程序:./startup.sh start

初次启动:
在这里插入图片描述
若已启动过:
在这里插入图片描述

3. 查看运行状态:./startup.sh stat 或 ./startup.sh check

运行状态:
在这里插入图片描述
停止状态:
在这里插入图片描述

4. 停止应用:./startup.sh

在这里插入图片描述

5. 强制杀掉应用进程:./startup.sh kill

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值