通过 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
标签:Shell,SpringBoot,pid,APP,jar,echo,file,操作,name
From: https://www.cnblogs.com/TMesh/p/18680730