想使用sh脚本来启动,停止,重启我们的jar服务, 顺便还要外置配置文件方便修改
示例server.sh如下,启动命令为sh server.sh start或restart或stop,修改其中的这几个参数即可:
SPB_OPTS 使用的配置profile类型
APP_LOCATION jar在linux中的绝对路径
APP_NAME 项目名称
#!/bin/bash
VM_OPTS="-Xms2048m -Xmx2048m"
SPB_OPTS="--spring.profiles.active=prod"
APP_LOCATION="/home/pmish/apps/m2k/m2k.jar"
APP_NAME="m2k"
PID_CMD="ps -ef |grep $APP_NAME |grep -v grep |awk '{print \$2}'"
start() {
echo "=============================start=============================="
PID=$(eval $PID_CMD)
if [[ -n $PID ]]; then
echo "$APP_NAME is already running,PID is $PID"
else
java $VM_OPTS -jar $APP_LOCATION $SPB_OPTS >/dev/null &
echo "java $VM_OPTS -jar $APP_LOCATION $SPB_OPTS >/dev/null "
PID=$(eval $PID_CMD)
if [[ -n $PID ]]; then
echo "Start $APP_NAME successfully,PID is $PID"
else
echo "Failed to start $APP_NAME !!!"
fi
fi
echo "=============================start=============================="
}
stop() {
echo "=============================stop=============================="
PID=$(eval $PID_CMD)
if [[ -n $PID ]]; then
kill -15 $PID
sleep 5
PID=$(eval $PID_CMD)
if [[ -n $PID ]]; then
echo "Stop $APP_NAME failed by kill -15 $PID,begin to kill -9 $PID"
kill -9 $PID
sleep 2
echo "Stop $APP_NAME successfully by kill -9 $PID"
else
echo "Stop $APP_NAME successfully by kill -15 $PID"
fi
else
echo "$APP_NAME is not running!!!"
fi
echo "=============================stop=============================="
}
restart() {
echo "=============================restart=============================="
stop
start
echo "=============================restart=============================="
}
status() {
echo "=============================status=============================="
PID=$(eval $PID_CMD)
if [[ -n $PID ]]; then
echo "$APP_NAME is running,PID is $PID"
else
echo "$APP_NAME is not running!!!"
fi
echo "=============================status=============================="
}
info() {
echo "=============================info=============================="
echo "APP_LOCATION: $APP_LOCATION"
echo "APP_NAME: $APP_NAME"
echo "VM_OPTS: $VM_OPTS"
echo "SPB_OPTS: $SPB_OPTS"
echo "=============================info=============================="
}
help() {
echo "start: start server"
echo "stop: shutdown server"
echo "restart: restart server"
echo "status: display status of server"
echo "info: display info of server"
echo "help: help info"
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
info)
info
;;
help)
help
;;
*)
help
;;
esac
exit $?
如果希望application.yml外置, 则应该在jar包同级目录新建一个config文件夹, 将application.yml放在config目录下即可