用于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
运行状态:
停止状态: