注释懒得写了,自己看吧,用的时候将 spring boot 项目打成 jar 包,新建 server.sh 内容如下,执行 ./server.sh restart
即可
#!/bin/bash
SERVICE_USER=$(whoami)
JDK_VERSION="jdk1.8.0_66"
SPRINGBOOTAPP_HOME=$(cd "$(dirname $0)"; pwd)
PROJECT_NAME=$(basename $(cd "$(dirname $0)"; pwd))
echo "============================================="
echo "Init JDK_VERSION = $JDK_VERSION"
echo "Init SPRINGBOOTAPP_HOME = $SPRINGBOOTAPP_HOME"
echo "Init PROJECT_NAME = $PROJECT_NAME"
echo "============================================="
. JDK $JDK_VERSION > /dev/null
LOG="$SPRINGBOOTAPP_HOME/$PROJECT_NAME.log"
RETVAL=0
pid_of_spring_boot() {
pgrep -f "java.*$PROJECT_NAME"
}
start() {
pid=`pid_of_spring_boot`
if [ -n "$pid" ]; then
echo "Project: $PROJECT_NAME (pid $pid) is running, kill first or restart."
return 1
fi
start=$(date +%s)
[ -e "$LOG" ] && cnt=`wc -l "$LOG" | awk '{ print $1 }'` || cnt=1
echo -n $"Starting $PROJECT_NAME: "
cd "$SPRINGBOOTAPP_HOME"
jarcount=`ls -l *.jar |wc -l`
if [ ! $jarcount = 1 ]; then
echo "more than one jar files in $SPRINGBOOTAPP_HOME"
return 1
fi
nohup java -jar "$SPRINGBOOTAPP_HOME/$(ls *.jar)" >> "$LOG" 2>&1 &
while { pid_of_spring_boot > /dev/null ; } &&
! { tail --lines=+$cnt "$LOG" | grep -q ' Started Application in' ; } ; do
sleep 1
done
pid=`pid_of_spring_boot`
RETVAL=$?
if [ $RETVAL = 0 ]; then
end=$(date +%s)
echo " success in $(( $end - $start )) seconds with (pid $pid)"
else
echo "Start failure, please check $LOG \n"
fi
echo
}
stop() {
printf "Stopping $PROJECT_NAME: "
pid=`pid_of_spring_boot`
[ -n "$pid" ] && kill $pid
RETVAL=$?
cnt=10
while [ $RETVAL = 0 -a $cnt -gt 0 ] &&
{ pid_of_spring_boot > /dev/null ; } ; do
sleep 1
((cnt--))
done
printf " success\n"
}
status() {
pid=`pid_of_spring_boot`
if [ -n "$pid" ]; then
echo "Project: $PROJECT_NAME (pid $pid) is running..."
return 0
fi
echo "Project: $PROJECT_NAME is stopped"
return 1
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit $RETVAL