维护JAVA服务脚本示例

维护JAVA服务脚本示例

start_env.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
#!/bin/bash

CURRENT_PATH=$(cd `dirname $0`; pwd)
PARENT_PATH=$(cd `dirname $0`; cd ..; pwd)
CURRENT_SERVICE_NAME=$(echo ${CURRENT_PATH} | awk -F '/' '{print $(NF)}')

# USE THOSE VARIABLE
GC_LOG_PATH=${CURRENT_PATH}/logs/${CURRENT_SERVICE_NAME}-gcdetail.log
COMMON_CONFIG_PATH=${PARENT_PATH}
CONFIG_PATH=${CURRENT_PATH}/config
JAR_PATH=${CURRENT_PATH}/jar
LOG_PATH=${CURRENT_PATH}/logs

## MODIFY FOLLOWING LINES
EXECUT_JAR="america-business-auth-impl-1.0-SNAPSHOT-all.jar"
DUBBO_PARAMETER="
-Dapp.id=america-business-auth
-Xdebug
-server
-Xmx512m
-Xms512m
-Xnoagent
-XX:+PrintGCDateStamps
-XX:+PrintGCDetails
-XX:NewRatio=2
-XX:SurvivorRatio=8
-XX:MetaspaceSize=128m
-XX:MaxMetaspaceSize=128m
-Xloggc:${GC_LOG_PATH}
-Ddubbo.registry.file=${JAR_PATH}/dubbo.cache
-Dconfig.path=file:${COMMON_CONFIG_PATH}/common.properties;file:${CONFIG_PATH}/config.properties
${JAR_PATH}/${EXECUT_JAR}
"
## STOP MODIFY

java -jar ${DUBBO_PARAMETER} | /usr/bin/cronolog $LOG_PATH/out.%Y%m%d.nohup >> /dev/null 2>&1 &

service_control.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash

#set -x

source /etc/profile

# USE THOSE VARIABLE
CURRENT_PATH=$(cd `dirname $0`; pwd)
PARENT_PATH=$(cd `dirname $0`; cd ..; pwd)
CURRENT_SERVICE_NAME=$(echo ${CURRENT_PATH} | awk -F '/' '{print $(NF)}')
STOP_WAIT_TIME=10
CURRENT_TIME=`date +%Y%m%d-%H%M%S`

DUMP_PATH=${CURRENT_PATH}/dump
LOG_PATH=${CURRENT_PATH}/logs



log() {
echo "$(date +"%Y-%m-%d %H:%M:%S"): $1"
}

exit_status() {
if [ $? == 0 ]
then
echo -e "\\033[1;32m[OK]\\033[0;39m"
else
echo -e "\\033[1;31m[Failed]\\033[0;39m"
log "Error"
exit 1
fi
}

check_user() {
if [ `whoami` != "app" || `whoami` != "root" ]; then
log "Please use app user to run this command."
false
exit_status
fi
}

stop_service() {

log "Stop Service Service: ${CURRENT_SERVICE_NAME}"
Service_NUM=`ps -ef|grep ${CURRENT_SERVICE_NAME}/jar/|grep -v grep|grep -v cronolog|grep -v $0|awk '{print $2}'|wc -l`
if [ $((Service_NUM)) -gt 1 ]
then
log "Service: ${CURRENT_SERVICE_NAME} run more than one process"
false
exit_status
fi
Service_PID=`ps -ef|grep ${CURRENT_SERVICE_NAME}/jar/|grep -v grep|grep -v cronolog|grep -v $0|awk '{print $2}'`
if [ x$Service_PID == x ];then
log "Service: ${CURRENT_SERVICE_NAME} process is NOT exist"
exit_status
else
log "KILL Service Service: ${CURRENT_SERVICE_NAME}"
kill $Service_PID
exit_status
fi
echo ""
log "Search Service Process ..."
Check=N
N=0
while [ $Check == "N" ]
do
N=$(($N+1))
Service_PID=`ps -ef|grep ${CURRENT_SERVICE_NAME}/jar/|grep -v grep|grep -v cronolog|grep -v $0|awk '{print $2}'`

if [ "$Service_PID" = "" ]
then
log "Not found Service PID alive"
break
else
log "$N: Check Service PID:$Service_PID, Sleep 1 Sec Wait Service Exit ..."
fi

if [ $((N)) -gt $STOP_WAIT_TIME ]
then
log "Force KILL Service: ${CURRENT_SERVICE_NAME}"
#jstack $Service_PID >> /var/log/jstack.log
#skill -9 $Service_PID
kill -9 $Service_PID
exit_status
break
fi
sleep 1
done
}

start_service() {

log "Start Service Service: ${CURRENT_SERVICE_NAME}"
Service_NUM=`ps -ef|grep ${CURRENT_SERVICE_NAME}/jar/ | grep -v grep |grep -v $0|awk '{print $2}'|wc -l`
if [ $((Service_NUM)) -gt 0 ]
then
log "Service: ${CURRENT_SERVICE_NAME} already running"
false
exit_status
fi

if [ ! -d $LOG_PATH ]; then
mkdir -p $LOG_PATH
fi

nohup /bin/bash $CURRENT_PATH/start_env.sh &>> /dev/null &

#exit_status
}

status() {

Service_NUM=`ps -ef|grep ${CURRENT_SERVICE_NAME}/jar/|grep -v grep|grep -v cronolog|grep -v $0|awk '{print $2}'|wc -l`
if [ $((Service_NUM)) -gt 1 ]
then
echo "Service run more than one process,please check"
false
exit_status
fi
Service_PID=`ps -ef|grep ${CURRENT_SERVICE_NAME}/jar/|grep -v grep|grep -v cronolog|grep -v $0|awk '{print $2}'`
if [ x$Service_PID == x ];then
echo "Service: ${CURRENT_SERVICE_NAME} process is NOT exist"
false
exit_status
else
Service_startTime=`ps -p ${Service_PID} -o lstart|grep -v "START"`
echo -e "service: ${CURRENT_SERVICE_NAME} started,\npid: ${Service_PID},\nstartTime: ${Service_startTime},\nlogPath: ${LOG_PATH}/out.`date +%Y%m%d`.nohup"
fi

}

dump() {
if [ ! -d $DUMP_PATH ]; then
mkdir -p $DUMP_PATH
fi

echo -e "Dumping the ${CURRENT_SERVICE_NAME} ...\c"
sudo /usr/local/jdk/bin/jstack $Service_PID > $DUMP_PATH/jstack-$Service_PID.${CURRENT_TIME}.dump 2>&1
echo -e ".\c"
sudo /usr/local/jdk/bin/jinfo $Service_PID > $DUMP_PATH/jinfo-$Service_PID.${CURRENT_TIME}.dump 2>&1
echo -e ".\c"
sudo /usr/local/jdk/bin/jstat -gcutil $Service_PID > $DUMP_PATH/jstat-gcutil-$Service_PID.${CURRENT_TIME}.dump 2>&1
echo -e ".\c"
sudo /usr/local/jdk/bin/jstat -gccapacity $Service_PID > $DUMP_PATH/jstat-gccapacity-$Service_PID.${CURRENT_TIME}.dump 2>&1
echo -e ".\c"
sudo /usr/local/jdk/bin/jmap $Service_PID > $DUMP_PATH/jmap-$Service_PID.${CURRENT_TIME}.dump 2>&1
echo -e ".\c"
sudo /usr/local/jdk/bin/jmap -heap $Service_PID > $DUMP_PATH/jmap-heap-$Service_PID.${CURRENT_TIME}.dump 2>&1
echo -e ".\c"
sudo /usr/local/jdk/bin/jmap -histo $Service_PID > $DUMP_PATH/jmap-histo-$Service_PID.${CURRENT_TIME}.dump 2>&1
echo -e ".\c"
if [ -r /usr/sbin/lsof ]; then
/usr/sbin/lsof -p $Service_PID > $DUMP_PATH/lsof-$Service_PID.${CURRENT_TIME}.dump
echo -e ".\c"
fi
echo -e "\nDump Complete, files in ${DUMP_PATH}\n"
}

tail_log() {
if [ ! -n "$1" ] ; then
tail_num=10
else
tail_num=$1
fi
tail -${tail_num}f ${LOG_PATH}/out.`date +%Y%m%d`.nohup
}

help() {
echo "USAGE: $0 {stop|start|restart|status|dump|log [tail line of numbers:200]|help}"
}

if [ $# -gt 0 ] ; then

case $1 in
start)
log "Will Start Service ..."
start_service
log "Sleep 3 Sec for Status ..."
sleep 3
status
;;
stop)
log "Will Stop Service ..."
stop_service
;;
restart)
log "Will Restart Service ..."

stop_service
log "Sleep 3 Sec"
sleep 3

start_service
log "Sleep 3 Sec for Status ..."
sleep 3
status
;;
status)
status
;;
dump)
check_user
status
if [ $? == 0 ]
then
dump
else
exit 1
fi

;;
log)
tail_log $2
;;
help)
help
;;
*)
echo "Bad Parameter: $1"
help
exit 1
;;
esac

else
help
exit 1;
fi

本文标题:维护JAVA服务脚本示例

文章作者:shuke

发布时间:2020年04月20日 - 15:04

最后更新:2020年04月20日 - 15:04

原始链接:https://shuke163.github.io/2020/04/20/%E7%BB%B4%E6%8A%A4JAVA%E6%9C%8D%E5%8A%A1%E8%84%9A%E6%9C%AC%E7%A4%BA%E4%BE%8B/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------

本文标题:维护JAVA服务脚本示例

文章作者:shuke

发布时间:2020年04月20日 - 15:04

最后更新:2020年04月20日 - 15:04

原始链接:https://shuke163.github.io/2020/04/20/%E7%BB%B4%E6%8A%A4JAVA%E6%9C%8D%E5%8A%A1%E8%84%9A%E6%9C%AC%E7%A4%BA%E4%BE%8B/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%