1.将对应的Spring boot打成jar包
2.在服务器目录上新建一个专门存放jar包的文件夹,我新建的目录为/root/zch/jar
将jar包放在该目录下,并将对应的脚本文件也放入该目录,我的脚本文件是server.sh,脚本文件对应的内容是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 1#!/bin/bash
2cd `dirname $0`
3CUR_SHELL_DIR=`pwd`
4CUR_SHELL_NAME=`basename ${BASH_SOURCE}`
5#修改这里jar包名即可
6JAR_NAME="xxxxxxxxxxxx.jar"
7JAR_PATH=$CUR_SHELL_DIR/$JAR_NAME
8#JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m"
9JAVA_MEM_OPTS=""
10#SPRING_PROFILES_ACTIV="-Dspring.profiles.active=eureka2"
11SPRING_PROFILES_ACTIV=""
12LOG_DIR=$CUR_SHELL_DIR/logs
13LOG_PATH=$LOG_DIR/${JAR_NAME}.log
14echo_help()
15{
16 echo -e "syntax: sh $CUR_SHELL_NAME start|stop"
17}
18if [ -z $1 ];then
19 echo_help
20 exit 1
21fi
22if [ ! -d "$LOG_DIR" ];then
23 mkdir "$LOG_DIR"
24fi
25if [ ! -f "$LOG_PATH" ];then
26 touch "$LOG_DIR"
27fi
28if [ "$1" == "start" ];then
29# check server
30 PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
31 if [ -n "$PIDS" ]; then
32 echo -e "ERROR: The $JAR_NAME already started and the PID is ${PIDS}."
33 exit 1
34 fi
35echo "Starting the $JAR_NAME..."
36
37 # start
38 nohup java $JAVA_MEM_OPTS -jar $SPRING_PROFILES_ACTIV $JAR_PATH >> $LOG_PATH 2>&1 &
39
40 COUNT=0
41 while [ $COUNT -lt 1 ]; do
42 sleep 1
43 COUNT=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}' | wc -l`
44 if [ $COUNT -gt 0 ]; then
45 break
46 fi
47 done
48 PIDS=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}'`
49 echo "${JAR_NAME} Started and the PID is ${PIDS}."
50 echo "You can check the log file in ${LOG_PATH} for details."
51
52elif [ "$1" == "stop" ];then
53
54 PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
55 if [ -z "$PIDS" ]; then
56 echo "ERROR:The $JAR_NAME does not started!"
57 exit 1
58 fi
59echo -e "Stopping the $JAR_NAME..."
60
61 for PID in $PIDS; do
62 kill $PID > /dev/null 2>&1
63 done
64
65 COUNT=0
66 while [ $COUNT -lt 1 ]; do
67 sleep 1
68 COUNT=1
69 for PID in $PIDS ; do
70 PID_EXIST=`ps --no-heading -p $PID`
71 if [ -n "$PID_EXIST" ]; then
72 COUNT=0
73 break
74 fi
75 done
76 done
77
78 echo -e "${JAR_NAME} Stopped and the PID is ${PIDS}."
79else
80 echo_help
81 exit 1
82fi
83
84
3.启动,关闭或者重启时通过命令cd /root/zch/jar进入该目录,再输入命令 ./server.sh start启动项目,输入命令./server.sh stop停止jar包,当输入命令报错 ./server.sh: Permission denied时,作如下处理即可:
chmod 777 ./*.sh,赋予文件的读写权限