Tomcat统一管理脚本

由于公司所用的服务大多都是Tomcat,且同时有几十个跑,为了统一进行管理,专门写了如下脚本,通过该脚本可以对Tomcat进行状态查询、代码版本号查询、启动、停止、重启、备份、回滚、发布更新。

当然前提必须是有如下规范的格式:

  1. 通过一个version.txt文件在首行声明当前代码的版本
  2. war包的文件名与业务名称一致(且大小写一致)
  3. 若要通过curl检测对应的http状态码,则需要统一端口号,否则需要另外通过一个文本指定每个业务的端口号进行一一对应。

分享出来仅供参考,如有不足之处,欢迎指出

脚本用法

实现效果

代码内容

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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/bash
####### Author: zyp19891128@163.com #########
####### error code specification #########
# Please update this documentation if new error code is added.
# 1 => reserved for script error
# 2 => bad usage
# 3 => bad user
# 4 => bad module
# 5 => bad version
# 6 => http status check failed
# 7 => bad update
# 8 => bad rollback file
####### V1.0 2017.03.30 #########
####### V1.1 2017.03.31 引入/root/scripts/MODULE文件,用于记录业务的目录名称与War包名称的对应关系,并优化业务模块合法性的检查.
####### V1.2 2017.03.31 增加Action:update,用于发布更新.
####### V1.3 2017.04.01 优化当webapps目录下为空或不存在version.txt文件时会卡住的问题.
####### V1.4 2017.07.29 新增lsbackup列出最近10个备份文件用于回滚时指定,并修改备份路径(若不存在则创建)

# variable from arguments
PROG_NAME=$0
MODULE=$1
ACTION=$2

# variable must be defined
TOMCAT_USER=tomcat
TOMCAT_HOME=/data/ztjy/$MODULE
TOMCAT_BIN="${TOMCAT_HOME}/bin"
TOMCAT_RUN="${TOMCAT_BIN}/startup.sh"
JAVA_HOME=/usr/java/jdk1.7.0_67
BACKUP_CENTER=10.30.206.112

# functions
usage() {
echo "Usage: $PROG_NAME <MODULE> {status|start|stop|restart|backup|lsbackup|update}"
echo " <MODULE> rollback [backfile]"
exit 2 # bad usage
}

check_pid() {
PID=`ps -ef | grep -w "$MODULE" |grep java | grep -v grep|awk '{print $2}'`
}

version() {
cd $TOMCAT_HOME/webapps
VER_FILE=`find ./ -name version.txt|tail -n 1`
if [ -n "$VER_FILE" ];then
VER=`grep -i "v" $VER_FILE |head -1|dos2unix|awk '{print $1}'`
echo -e "The Version of $MODULE is:\033[33m $VER \033[0m"
else
echo -e "\033[31mversion error:\033[0m version.txt is not exist"
fi
}

status() {
check_pid
if [ -n "$PID" ]; then
echo -e "$MODULE\033[32m (PID $PID)\033[0m is running"
else
echo "$MODULE is stoped"
fi
}

start() {
check_pid
if [ -n "$PID" ]; then
echo -e "$MODULE is already running\033[32m (PID $PID)\033[0m. Start aborted."
exit 1
else
echo "Starting $MODULE ..."
su - $TOMCAT_USER -c "${TOMCAT_RUN}"
fi
}

stop() {
check_pid
if [ -z "$PID" ]; then
echo "$MODULE is stoped. Stop aborted."
exit 1
else
echo "Stopping $MODULE ..."
echo $PID | xargs kill -SIGUSR1
fi
}

restart() {
check_pid
if [ -n "$PID" ]; then
echo "Stopping $MODULE ..."
echo $PID | xargs kill -SIGUSR1
fi
status
sleep 1
start
sleep 5
status
#http_status
}

http_status() {
check_pid
if [ -n "$PID" ];then
HTTP_CODE=`curl -skf -m 10 --retry 12 -o /dev/null -w %{http_code} $URL`
if [ x$HTTP_CODE != x200 ];then
echo -e "check $URL \033[31m failed with HTTP_CODE:${HTTP_CODE} \033[0m after wait 120 seconds."
echo -e "you can check again with \033[33m \"curl -skf -m 10 --retry 6 -o /dev/null -w %{http_code} $URL\" \033[0m"
exit 6
else
echo -e "check $URL \033[32m success with HTTP_CODE:${HTTP_CODE} \033[0m"
fi
fi
}

backup() {
version
if [ ! -d "/data/back/$MODULE" ];then
mkdir -p /data/back/$MODULE
fi
cd $TOMCAT_HOME/webapps/
if [ -d "$TOMCAT_HOME/webapps/$WAR" ];then
BACKUP_FILE=$MODULE-$VER-`date +%m%d%H%M`.tar.gz
echo -e "backup the $TOMCAT_HOME/webapps/$WAR directory to \033[32m /data/back/$MODULE/$BACKUP_FILE \033[0m"
tar zcf /data/back/$MODULE/$BACKUP_FILE $WAR
else
echo -e "\033[31m /data/back/$MODULE/$WAR \033[0m is not exist"
fi
}

lsbackup(){
ls -shrt /data/back/$MODULE/ | tail
}

rollback() {
if [ -f "/data/back/$MODULE/$ROLLBACK_FILE" ];then
echo -e "++++++++++++++++++++++ `date +%Y/%m/%d-%H:%M:%S` Begin Rollback ....."
echo -e "rollback the $MODULE's version to:\033[32m $VER backup \033[0m from \033[33m $ROLLBACK_FILE \033[0m"
cd $TOMCAT_HOME/webapps/ && rm -rf $WAR*
tar zxf /data/back/$MODULE/$ROLLBACK_FILE
echo -e "++++++++++++++++++++++ `date +%Y/%m/%d-%H:%M:%S` Rollback complate."
else
echo -e "\033[31m $ROLLBACK_FILE \033[0m is not exist"
exit 8
fi
}

update() {
HTTP_CODE=`curl -skf -m 2 -o /dev/null -w %{http_code} http://$BACKUP_CENTER/update/$MODULE/$WAR.war`
if [ x$HTTP_CODE == x200 ];then
echo -e "++++++++++++++++++++++ `date +%Y/%m/%d-%H:%M:%S` Begin update ....."
echo -e "update the $MODULE from \033[33m http://$BACKUP_CENTER/update/$MODULE/$WAR.war \033[0m"
cd $TOMCAT_HOME/webapps/ && rm -rf $WAR*
wget -q -O $TOMCAT_HOME/webapps/$WAR.war http://$BACKUP_CENTER/update/$MODULE/$WAR.war
echo -e "++++++++++++++++++++++ `date +%Y/%m/%d-%H:%M:%S` update complate."
else
echo -e "Check the URL is valid (\033[31mhttp://$BACKUP_CENTER/update/$MODULE/$WAR.war\033[0m is not exist)"
echo -e "remove the BACKUP_FILE /data/back/$MODULE/$BACKUP_FILE"
rm -f /data/back/$MODULE/$BACKUP_FILE
exit 7 #bad update
fi
}

# main
# check if TOMCAT_USER exist
id $TOMCAT_USER >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "No such user: $TOMCAT_USER"
exit 3
fi

# check if dos2unix is installed
which dos2unix >/dev/null 2>&1
if [ $? -ne 0 ];then
echo "Please wait to install the package of dos2unix"
yum -y -q install dos2unix >/dev/null 2>&1
fi

# if the ACTION is not rollback and argument number must be 2
if [ "$ACTION" != "rollback" -a $# -ne 2 ];then
usage
fi

# if the ACTION is not rollback and argument number must be 2 or 3
if [ "$ACTION" = "rollback" ];then
if [ $# -eq 2 ];then
echo -e "Please input the file which will rollback following command \033[33mappctl.sh $MODULE lsbackup \033[0m"
exit 8 # bad rollback file
elif [ $# -eq 3 ];then
ROLLBACK_FILE="$3"
else
usage
fi
fi

# check the MODULE is valid
WAR=`grep -w $MODULE /root/scripts/MODULE | awk -F ':' '{print $2}'`
if [ ! -d $TOMCAT_HOME -o "$WAR" = "" ];then
echo -e "\033[31m $MODULE is invalid \033[0m"
exit 4 #bad module
fi

chown -R $TOMCAT_USER:$TOMCAT_USER $TOMCAT_HOME

# ACTION
case "$ACTION" in
status)
status
version
#http_status
;;
start)
start
sleep 5
status
#http_status
;;
stop)
stop
sleep 5
status
;;
restart)
restart
version
;;
backup)
backup
;;
lsbackup)
lsbackup
;;
rollback)
rollback
echo -e "++++++++++++++++++++++ Now will restart $MODULE"
restart
version
;;
update)
backup
update
echo -e "++++++++++++++++++++++ Now will restart $MODULE"
restart
version
;;
*)
usage
;;
esac
坚持原创技术分享,您的支持将鼓励我继续创作!
0%