# 注意:直接vim创建,不要在window下新建文本改名上传# 因windows和linux编码格式问题,会出现脚本无法运行,no such file or directory的错误提示vim xxx.sh
1
2
3
2. xxx.sh脚本内容,实际项目使用,只需修改jar包的名称:xxxxxx.jar
#!/bin/bashcd`dirname $0`CUR_SHELL_DIR=`pwd`CUR_SHELL_NAME=`basename ${BASH_SOURCE}`#修改这里jar包名即可JAR_NAME="xxxxxx.jar"JAR_PATH=$CUR_SHELL_DIR/$JAR_NAME#JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m"JAVA_MEM_OPTS=""#SPRING_PROFILES_ACTIV="-Dspring.profiles.active=dev"SPRING_PROFILES_ACTIV=""LOG_DIR=$CUR_SHELL_DIR/logs
LOG_PATH=$LOG_DIR/${JAR_NAME}.log
echo_help(){echo-e"syntax: sh $CUR_SHELL_NAME start|stop"}if[-z$1];then
echo_help
exit1fiif[!-d"$LOG_DIR"];thenmkdir"$LOG_DIR"fiif[!-f"$LOG_PATH"];thentouch"$LOG_DIR"fiif["$1"=="start"];then# check serverPIDS=`ps --no-heading -Cjava-f--width1000|grep $JAR_NAME |awk'{print $2}'`if[-n"$PIDS"];thenecho-e"ERROR: The $JAR_NAME already started and the PID is ${PIDS}."exit1fiecho"Starting the $JAR_NAME..."# startnohupjava$JAVA_MEM_OPTS-jar$SPRING_PROFILES_ACTIV$JAR_PATH>>$LOG_PATH2>&1&COUNT=0while[$COUNT-lt1];dosleep1COUNT=`ps --no-heading -Cjava-f--width1000|grep"$JAR_NAME"|awk'{print $2}'|wc-l`if[$COUNT-gt0];thenbreakfidonePIDS=`ps --no-heading -Cjava-f--width1000|grep"$JAR_NAME"|awk'{print $2}'`echo"${JAR_NAME} Started and the PID is ${PIDS}."echo"You can check the log file in ${LOG_PATH} for details."elif["$1"=="stop"];thenPIDS=`ps --no-heading -Cjava-f--width1000|grep $JAR_NAME |awk'{print $2}'`if[-z"$PIDS"];thenecho"ERROR:The $JAR_NAME does not started!"exit1fiecho-e"Stopping the $JAR_NAME..."forPIDin$PIDS;dokill$PID> /dev/null 2>&1doneCOUNT=0while[$COUNT-lt1];dosleep1COUNT=1forPIDin$PIDS;doPID_EXIST=`ps --no-heading -p $PID`if[-n"$PID_EXIST"];thenCOUNT=0breakfidonedoneecho-e"${JAR_NAME} Stopped and the PID is ${PIDS}."else
echo_help
exit1fi
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
3. 给xxx.sh赋予执行权限
chmod +x xxx.sh
1
4. xxx.sh脚本的使用
启动jar包
./xxx.sh start
停止 jar包
./xxx.sh stop
查看帮助
./xxx.sh help