常用-启动jar包的Shell脚本
Mr.Lee 2024-11-23 21:26:00 JavaLinuxShell
常用Shell脚本-启动 jar
包
# 物理机启动
#!/bin/sh
# 可以根据实际情况进行修改
APP_NAME=ksd-manager
SERVER_PORT=8081
ACTIVE=dev
JAR_PATH='/opt/apps/ksd-manager'
JAR_NAME=ksd-manager.jar
#================================
JAR_PID=$JAR_PATH/$JAR_NAME\.pid
LOG_FILE=logs
JAVA_OPTS="-Xms512m -Xmx512m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=1024m -XX:ParallelGCThreads=4 -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=utf-8 -Dspring.profiles.active=$ACTIVE -Dspring.config.location=$JAR_PATH/config/"
# 判断进程是否存在
is_exist() {
pid=`ps -ef|grep $JAR_NAME|grep -v grep|awk '{print $2}' `
if [ -z "${pid}" ]; then
return 0
else
return 1
fi
}
# 启动服务
start() {
is_exist
if [ $? -eq "1" ]; then
echo "$APP_NAME pid is ${pid}"
else
echo $JAVA_OPTS
nohup java $JAVA_OPTS -jar $JAR_PATH/$JAR_NAME > $JAR_PATH/$LOG_FILE/run.log 2>&1 &
echo $! > $JAR_PID
echo " $APP_NAME pid is $! "
# tail -100f $JAR_PATH/$LOG_FILE/run.log
fi
}
# 停止服务
stop() {
# is_exist
pidf=$(cat $JAR_PID)
# echo "$pidf"
echo "pid = $pidf begin kill $pidf"
kill $pidf
rm -rf $JAR_PID
sleep 2
is_exist
if [ $? -eq "1" ]; then
echo "pid = $pid begin kill -9 $pid"
kill -9 $pid
sleep 2
echo "$APP_NAME "
else
echo "$APP_NAME "
fi
}
# 查看服务状态
status() {
is_exist
if [ $? -eq "1" ]; then
echo "$APP_NAME pid is ${pid}"
else
echo "$APP_NAME "
fi
}
# 重新启动服务
restart() {
stop
start
}
usage() {
echo "Usage: sh app.sh [ start | stop | restart | status ]"
exit 1
}
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
restart
;;
'status')
status
;;
*)
usage
;;
esac
exit 0
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
可以配合
System.Service
使用systemctl
# 容器内启动
#!/bin/bash
set -x
# JVM Configuration
JAVA_OPT="${JAVA_OPT} -Xms${JVM_XMS} -Xmx${JVM_XMX} -XX:MetaspaceSize=${JVM_MS} -XX:MaxMetaspaceSize=${JVM_MMS}"
## system properties
# JAVA_OPT="${JAVA_OPT} -Dfile.encoding=utf-8 -Dspring.cloud.nacos.discovery.group=${NACOS_GROUP}"
JAVA_OPT="${JAVA_OPT} -Dspring.cloud.nacos.discovery.namespace=${NACOS_NAMESPACE}"
JAVA_OPT="${JAVA_OPT} -Dspring.cloud.nacos.discovery.server-addr=${NACOS_SERVER_ADDR}"
JAVA_OPT="${JAVA_OPT} -Dspring.cloud.nacos.username=${NACOS_USERNAME}"
JAVA_OPT="${JAVA_OPT} -Dspring.cloud.nacos.password=${NACOS_PASSWORD}"
if [ "${APP_NAME}" = "omm-gateway" ]; then
APP_PORT=58082
APP_JAR="omm-gateway.jar"
fi
if [ "${APP_NAME}" = "omm-indicator-data" ]; then
APP_PORT=58083
APP_JAR="omm-indicator-data.jar"
fi
if [ "${APP_NAME}" = "omm-monitor-view" ]; then
APP_PORT=58084
APP_JAR="omm-monitor-view.jar"
fi
if [ "${APP_NAME}" = "omm-alert-manage" ]; then
APP_PORT=58085
APP_JAR="omm-alert-manage.jar"
fi
if [ "${APP_NAME}" = "omm-dispose-manage" ]; then
APP_PORT=58086
APP_JAR="omm-dispose-manage.jar"
fi
if [ "${APP_NAME}" = "omm-system-config" ]; then
APP_PORT=58087
APP_JAR="omm-system-config.jar"
fi
JAVA_OPT="${JAVA_OPT} -Dserver.port=${APP_PORT}"
JAVA_OPT="${JAVA_OPT} -jar ${APP_JAR}"
exec java ${JAVA_OPT}
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
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