-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinit-java.sh
More file actions
executable file
·470 lines (422 loc) · 12.8 KB
/
Copy pathinit-java.sh
File metadata and controls
executable file
·470 lines (422 loc) · 12.8 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/bin/bash
#
# $Id: javainitscript 164 2011-06-24 06:04:55Z glenn $
#
# Startup script for Jakarta Tomcat, Lferay, JBoss, or potentially other java apps
#
# chkconfig: 345 94 16
# description: Jakarta Tomcat Java Servlet/JSP Container or JBoss
# this script can be called tomcat or jboss. Depending on what you
# name the file it will start up either tomcat or jboss.
# to enable on bootup on redhat "chkconfig --level 35 tomcat on"
# to enable on bootup on debian "update-rc.d tomcat defaults" or the
# slightly longer (but better run levels) "update-rc.d tomcat start 91 2 3 4 5 . stop 20 0 1 6 ."
# Change the chkconfig/update-rc.d from tomcat to jboss if that is what you actually
# want to run
### BEGIN INIT INFO
# Provides: javainitscript
# Required-Start: $syslog $time
# Required-Stop: $syslog $time
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Java services
# Description: Java services (like tomcat, liferay, jboss, etc)
### END INIT INFO
if [ -e /etc/debian_version ]; then
. /lib/lsb/init-functions
elif [ -e /etc/init.d/functions ] ; then
. /etc/init.d/functions
fi
if ! type log_daemon_msg 2>&1 |grep -qai function ; then
# no lsb setup? no problem. we will add them in
log_use_fancy_output () {
TPUT=/usr/bin/tput
EXPR=/usr/bin/expr
if [ -t 1 ] && [ "x$TERM" != "" ] && [ "x$TERM" != "xdumb" ] && [ -x $TPUT ] && [ -x $EXPR ] && $TPUT hpa 60 >/dev/null 2>&1 && $TPUT setaf 1 >/dev/null 2>&1; then
[ -z $FANCYTTY ] && FANCYTTY=1 || true
else
FANCYTTY=0
fi
case "$FANCYTTY" in
1|Y|yes|true) true;;
*) false;;
esac
}
log_success_msg () {
if [ -n "${1:-}" ]; then
log_begin_msg $@
fi
log_end_msg 0
}
log_failure_msg () {
if [ -n "${1:-}" ]; then
log_begin_msg $@
fi
log_end_msg 1 || true
}
log_warning_msg () {
if [ -n "${1:-}" ]; then
log_begin_msg $@
fi
log_end_msg 255 || true
}
#
# NON-LSB HELPER FUNCTIONS
#
# int get_lsb_header_val (char *scriptpathname, char *key)
get_lsb_header_val () {
if [ ! -f "$1" ] || [ -z "${2:-}" ]; then
return 1
fi
LSB_S="### BEGIN INIT INFO"
LSB_E="### END INIT INFO"
sed -n "/$LSB_S/,/$LSB_E/ s/# $2: \(.*\)/\1/p" $1
}
# int log_begin_message (char *message)
log_begin_msg () {
if [ -z "${1:-}" ]; then
return 1
fi
echo -n "$@"
}
# Sample usage:
# log_daemon_msg "Starting GNOME Login Manager" "gdm"
#
# On Debian, would output "Starting GNOME Login Manager: gdm"
# On Ubuntu, would output " * Starting GNOME Login Manager..."
#
# If the second argument is omitted, logging suitable for use with
# log_progress_msg() is used:
#
# log_daemon_msg "Starting remote filesystem services"
#
# On Debian, would output "Starting remote filesystem services:"
# On Ubuntu, would output " * Starting remote filesystem services..."
log_daemon_msg () {
if [ -z "${1:-}" ]; then
return 1
fi
log_daemon_msg_pre "$@"
if [ -z "${2:-}" ]; then
echo -n "$1:"
return
fi
echo -n "$1: $2"
log_daemon_msg_post "$@"
}
# #319739
#
# Per policy docs:
#
# log_daemon_msg "Starting remote file system services"
# log_progress_msg "nfsd"; start-stop-daemon --start --quiet nfsd
# log_progress_msg "mountd"; start-stop-daemon --start --quiet mountd
# log_progress_msg "ugidd"; start-stop-daemon --start --quiet ugidd
# log_end_msg 0
#
# You could also do something fancy with log_end_msg here based on the
# return values of start-stop-daemon; this is left as an exercise for
# the reader...
#
# On Ubuntu, one would expect log_progress_msg to be a no-op.
log_progress_msg () {
if [ -z "${1:-}" ]; then
return 1
fi
echo -n " $@"
}
# int log_end_message (int exitstatus)
log_end_msg () {
# If no arguments were passed, return
if [ -z "${1:-}" ]; then
return 1
fi
retval=$1
log_end_msg_pre "$@"
# Only do the fancy stuff if we have an appropriate terminal
# and if /usr is already mounted
if log_use_fancy_output; then
RED=`$TPUT setaf 1`
YELLOW=`$TPUT setaf 3`
NORMAL=`$TPUT op`
else
RED=''
YELLOW=''
NORMAL=''
fi
if [ $1 -eq 0 ]; then
echo "."
elif [ $1 -eq 255 ]; then
/bin/echo -e " ${YELLOW}(warning).${NORMAL}"
else
/bin/echo -e " ${RED}failed!${NORMAL}"
fi
log_end_msg_post "$@"
return $retval
}
log_action_msg () {
echo "$@."
}
log_action_begin_msg () {
echo -n "$@..."
}
log_action_cont_msg () {
echo -n "$@..."
}
log_action_end_msg () {
log_action_end_msg_pre "$@"
if [ -z "${2:-}" ]; then
end="."
else
end=" ($2)."
fi
if [ $1 -eq 0 ]; then
echo "done${end}"
else
if log_use_fancy_output; then
RED=`$TPUT setaf 1`
NORMAL=`$TPUT op`
/bin/echo -e "${RED}failed${end}${NORMAL}"
else
echo "failed${end}"
fi
fi
log_action_end_msg_post "$@"
}
# Hooks for /etc/lsb-base-logging.sh
log_daemon_msg_pre () { :; }
log_daemon_msg_post () { :; }
log_end_msg_pre () { :; }
log_end_msg_post () { :; }
log_action_end_msg_pre () { :; }
log_action_end_msg_post () { :; }
fi
# how long to wait for the app to startup before saying 'its probably up'
STARTWAITTIMES=45
# figure out what to do based on the name of this script
if echo $0 | grep -qai tomcat; then
HOMEDIR=/usr/local/tomcat
TOMCAT_USER=tomcat
APPNAME=Tomcat
elif echo $0 | grep -qai alfresco; then
HOMEDIR=/usr/local/alfresco/tomcat
TOMCAT_USER=alfresco
APPNAME=Alfresco
elif echo $0 | grep -qai jboss; then
HOMEDIR=/usr/local/jboss
TOMCAT_USER=jboss
APPNAME=JBoss
STARTWAITTIMES=60
elif echo $0 | grep -qai liferay; then
tomcatloc=$( ls /usr/local/liferay | grep tomcat )
HOMEDIR=/usr/local/liferay/$tomcatloc
TOMCAT_USER=liferay
APPNAME=Liferay
if [ -d /home/$TOMCAT_USER ]; then
#since liferay can put some stuff (e.g. lucene search indexes)
chown -R $TOMCAT_USER:$TOMCAT_USER /home/$TOMCAT_USER
fi
STARTWAITTIMES=120
else
log_failure_msg "Unknown startup script name $0"
exit 1
fi
# something so we can test if the app is fully started
TESTURL=http://127.0.0.1:8080/
if [ -e $HOMEDIR/initscript/testurl ]; then
TESTURL=$(cat $HOMEDIR/initscript/testurl)
fi
#Necessary environment variables
[ -e /usr/java/jdk/bin/java ] && export JAVA_HOME=/usr/java/jdk
#export LD_KERNEL_ASSUME="2.2.5"
# set application specific defaults
if [ "$APPNAME" = "Tomcat" -o "$APPNAME" = "Liferay" -o "$APPNAME" = "Alfresco" ]; then
export CATALINA_HOME=$HOMEDIR
INITSCRIPT=$HOMEDIR/bin/catalina.sh
RUNCOMMAND="export CATALINA_HOME=$CATALINA_HOME; $INITSCRIPT start"
STOPCOMMAND="$INITSCRIPT stop"
GREPSTRING="$(basename $HOMEDIR).*[o]rg.apache.catalina.startup.Bootstrap start"
LOGFILE=$HOMEDIR/logs/catalina.out
elif [ "$APPNAME" = "JBoss" ]; then
. /usr/local/jboss/bin/run.conf
INITSCRIPT=$HOMEDIR/bin/run.sh
if [ -z "$JBOSS_CONFIGURATION" ]; then
JBOSS_CONFIGURATION=default
fi
RUNCOMMAND="bash $INITSCRIPT -c $JBOSS_CONFIGURATION $JBOSS_OPTIONS "
STOPCOMMAND="bash $HOMEDIR/bin/shutdown.sh $JBOSS_SHUTDOWN_OPTIONS "
GREPSTRING="[o]rg.jboss.Main"
LOGFILE=$HOMEDIR/server/$JBOSS_CONFIGURATION/log/jbconsole.log
else
log_failure_msg "Only JBoss and Tomcat are recognised. Not $APPNAME"
exit 1
fi
# makes it a bit easier to find if the process is running. e.g. put a -Dsomeval=Y
# into the JAVA_OPTS for the program. e.g. in catalina.sh or setenv.sh or run.conf
if [ -e $HOMEDIR/initscript/grepstring ]; then
GREPSTRING=$(cat $HOMEDIR/initscript/grepstring)
fi
# allow overriding of these variables without script changes via a conf file
if [ -e $HOMEDIR/initscript/initscript.conf ] ; then
source $HOMEDIR/initscript/initscript.conf
fi
#if [ "$APPNAME" = "Liferay" ]; then
# TOMCAT_VER_DIR=$(ls $HOMEDIR | grep tomcat)
# if [ -d "$TOMCAT_VER_DIR/logs" ]; then
# LOGFILE=$HOMEDIR/$TOMCAT_VER_DIR/logs/ltconsole.out
# fi
#fi
#Check for init script
if [ ! -f $INITSCRIPT ]; then
log_failure_msg "$APPNAME not available... (no $INITSCRIPT)"
exit 1
fi
if ! id "$TOMCAT_USER" >/dev/null; then
log_failure_msg "$TOMCAT_USER is not a user. Please create a user account first."
exit 1
fi
setpslist() {
PSLIST=$(ps a --width=1000 --User "$TOMCAT_USER" -o pid,user,command | grep "$GREPSTRING" | grep -v PID | awk '{printf $1 " "}')
}
start() {
setpslist
log_daemon_msg "Starting" "$APPNAME"
if [ ! -z "$PSLIST" ]; then
log_warning_msg "$APPNAME already running, can't start it"
log_end_msg 1
return 1
fi
if [ ! -e "$LOGFILE" -a ! -e "`dirname \"$LOGFILE\"`" ]; then
#log_action_msg "mkdir -p $(dirname "$LOGFILE")"
mkdir -p $(dirname "$LOGFILE")
fi
if [ -e "$LOGFILE" ]; then
#log_action_msg "mv $LOGFILE $LOGFILE.old"
mv $LOGFILE{,.old}
fi
chown -R $TOMCAT_USER $HOMEDIR
chmod -R g+w $HOMEDIR
exec su - -p --shell=/bin/sh $TOMCAT_USER -c "cd $(dirname $INITSCRIPT); $RUNCOMMAND >\"$LOGFILE\"" 2>&1 &
local starttime=$(date +"%s")
# wait a bit for the app to startup
while true; do
sleep 3
local now=$(date +"%s")
if wget --tries=1 --timeout=1 --server-response -O - $TESTURL 2>&1 | grep -qai " HTTP/1.1 "; then
log_end_msg 0
break
fi
# process not starting (cf. http response not happening)
if [ $(($now - 15 )) -gt $starttime ]; then
setpslist
if [ -z "$PSLIST" ]; then
log_failure_msg "Java process not starting. Last few lines from the startup log follow:"
log_failure_msg "$(tail -n 4 $LOGFILE)"
log_end_msg 1
return 1
fi
fi
if [ $(($now - $STARTWAITTIMES)) -gt $starttime ]; then
log_warning_msg "$APPNAME startup taking too long, not getting a response on $TESTURL, giving up"
log_end_msg 0
return 0
fi
log_progress_msg .
done
}
killprocesses() {
log_daemon_msg "Killing" "$APPNAME"
setpslist
if [ -z "$PSLIST" ]; then
log_progress_msg "$APPNAME not running, no need to kill it"
log_end_msg 0
return
fi
kill -9 $PSLIST
log_end_msg 0
}
stop() {
log_daemon_msg "Stopping" "$APPNAME"
setpslist
if [ -z "$PSLIST" ]; then
log_progress_msg "$APPNAME not running, no need to stop it"
log_end_msg 0
fi
waslistening=N
needtokill=N
if wget --tries=1 --timeout=1 --server-response -O - $TESTURL 2>&1 | grep -qai " HTTP/1.1 "; then
waslistening=Y
fi
suoutput=$(su - --shell=/bin/bash -p $TOMCAT_USER -c "$STOPCOMMAND" 2>&1)
local starttime=$(date +"%s")
# wait a while for the app to shutdown gracefully, else kill it
while true; do
sleep 3
local now=$(date +"%s")
setpslist
if [ -z "$PSLIST" ]; then
log_end_msg 0
return 0
fi
if echo $suoutput | egrep -qai "(Refused|Address already in use)" ; then
log_warning_msg "'stop' signal refused, killing $APPNAME."
kill -SIGTERM $PSLIST
elif [ $(($now - 80)) -gt $starttime ]; then
log_warning_msg "Graceful shutdown taking too long, killing it.";
kill -SIGKILL $PSLIST
elif [ $(($now - 50)) -gt $starttime ]; then
log_warning_msg "Graceful shutdown taking too long, terminating it.";
kill -SIGTERM $PSLIST
elif [ "$needtokill" = "Y" ]; then
log_progress_msg "Killing. "
kill -SIGKILL $PSLIST
elif [ "$waslistening" = "Y" -a "$needtokill" = "N" ]; then
if ! wget --tries=1 --timeout=1 --server-response -O - $TESTURL 2>&1 | grep -qai " HTTP/1.1 " ; then
log_progress_msg "Stopped listening on http, but not shutting down fully. "
needtokill=Y
sleep 10
fi
fi
# echo -n $(echo $PSLIST | wc -w) " "
done
}
status() {
setpslist
if [ ! -z "$PSLIST" ]; then
local MSG="$APPNAME ( PIDs $PSLIST ) is running."
if wget --tries=1 --timeout=1 --server-response -O - $TESTURL 2>&1 | grep -qai " HTTP/1.1 "; then
log_success_msg "$MSG And listening on $TESTURL."
else
log_warning_msg "$MSG But not responding on $TESTURL."
fi
else
log_failure_msg "$APPNAME is not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
kill)
killprocesses
;;
killstart)
killprocesses
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status|kill|killstart}"
exit 1
esac
exit $?