Shell SpringBoot 操作

通过 shell 脚本来操作 SpringBoot,检查程序是否在运行,启动程序,停止程序,重启程序,输出程序状态

#!/bin/bash
#这里可替换为你自己的执行程序,其他代码无需更改
APP_NAME="$2"

APP_DIR=/application
#APP_DIR=`pwd`
 
#使用说明,用来提示输入参数
usage() {
 echo "Usage: sh xxx.sh [start|stop|restart|status|exist]"
 exit 1
}
 
#检查程序是否在运行
is_exist(){
 echo $1
 jar_name=$1
 file_name=${jar_name:0:${#jar_name}-4}
 echo jar_name:${jar_name}
 echo file_name:${file_name}
 pid=`ps -ef|grep $jar_name|grep -v grep|awk '{print $2}' `

 echo "ps number is: ${pid}"
 echo ""
 #如果不存在返回1,存在返回0 
 if [ -z "${pid}" ]; then
 return 1
 else
 return 0
 fi
}
 
#启动方法
start(){
 is_exist $1
 if [ $? -eq "0" ]; then
 echo "$1 is already running. pid=${pid} ."
 else
 nohup java -jar -Duser.timezone=GMT+8 $APP_DIR/$1 > $APP_DIR/log.out 2>&1 &
 #nohup java -jar $APP_DIR/$1
 echo "$1 start success"
 fi
}
 
#停止方法
stop(){
 is_exist $1
 if [ $? -eq "0" ]; then
 kill -9 $pid
 echo "$1 is successfull killed"
 else
 echo "$1 is not running"
 fi 
}
 
#输出运行状态
status(){
 is_exist $1
 if [ $? -eq "0" ]; then
 echo "$1 is running. Pid is ${pid}"
 else
 echo "$1 is NOT running."
 fi
}
 
#重启
restart(){
 stop $1
 start $1
}

echo "1=$1"
echo "2=$2"
echo "APP_NAME=$APP_NAME"
echo "APP_DIR=$APP_DIR"
echo ""
 
#根据输入参数,选择执行对应方法,不输入则执行使用说明

for file in $(ls *.jar)
do
case "$1" in
 "start")
 start $file
 ;;
 "stop")
 stop $file
 ;;
 "status")
 status $file
 ;;
 "restart")
 restart $file
 ;;
 "exist")
 is_exist $file
 ;;
 *)
 usage
 ;;
esac
done
posted @   Thousand_Mesh  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示