新建启动脚本文件start.sh
,脚本内容如下:
#!/bin/bash
# 激活的Spring配置文件
SPRING_PROFILE="-Dspring.profiles.active=prod"
# 应用程序,全路径加jar包名
APP_NAME="/xxx/xxx/xxx.jar"
# 虚拟机参数,根据需求添加修改
JVM="-Xms256m -Xmx2048m"
# 远程Debug调试配置
REMOTE_CONFIG="-Xdebug -Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=n"
# 使用说明
function usage() {
echo "使用说明:sh 脚本名称.sh [start | stop | restart | debug | status]"
exit 1
}
# 检查程序是否在运行
function is_exist() {
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
# 如果不存在则返回1,存在则返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
# 启动应用
function start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid}"
else
nohup java ${JVM} ${SPRING_PROFILE} -jar ${APP_NAME} >/dev/null 2>&1 &
echo "${APP_NAME} start success"
fi
}
# 停止应用
function stop(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} shutting down. pid=${pid}"
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
# 输出应用运行状态
function status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. pid=${pid}"
else
echo "${APP_NAME} is not running"
fi
}
# 重启
function restart(){
stop
start
}
# 远程调试Debug模式
function debug() {
echo " start remote debug mode .........."
nohup java ${REMOTE_CONFIG} -jar $APP_NAME >/dev/null 2>&1 &
}
# 根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"debug")
debug
;;
"stop")
stop
;;
"restart")
restart
;;
"status")
status
;;
*)
usage
;;
esac
exit 0
启动应用 sh start.sh start
停止应用 sh start.sh stop
重启应用 sh start.sh restart
查看状态 sh start.sh status
远程debug模式启动应用 sh start.sh debug