-
Notifications
You must be signed in to change notification settings - Fork 72
/
tomcat7.init
153 lines (134 loc) · 3.24 KB
/
tomcat7.init
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
#!/bin/bash
# Tomcat7 init script.
#
# chkconfig: - 80 20
# description: Start/stop script for Tomcat6
# Source function library.
. /etc/rc.d/init.d/functions
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
[ "${NETWORKING}" = "no" ] && exit 0
prog="tomcat7"
[ -f /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
TOMCATDEBUG=
TOMCATSECURITY=${SECURITY:+"-security"}
CATALINA_HOME=${CATALINA_HOME:="/usr/share/$prog"}
RUNAS_USER=${RUNAS_USER:="tomcat"}
WAITFOR=${WAITFOR:=3}
CATALINA_PID="/var/run/${prog}.pid"
RUN_SCRIPT="${CATALINA_HOME}/bin/catalina.sh"
CDIR=$(pwd)
JUNK=${JAVA_HOME:?"MUST BE CONFIGURED IN /etc/sysconfig/$prog"}
if [ ! -d $JAVA_HOME ]; then
echo "$JAVA_HOME doesn't exist!"
exit 1
fi
if [ ! -d $CATALINA_HOME ]; then
echo CATALINA_HOME does not exist as a valid directory: $CATALINA_HOME
exit 1
fi
export CATALINA_HOME CATALINA_PID JAVA_HOME JAVA_OPTS
cleanup() {
rm -f ${CATALINA_PID}
}
echoFailure() {
echo_failure
echo
}
echoSuccess() {
echo_success
echo
}
start() {
echo -n "Starting $prog..."
STATUS=$(status >/dev/null)
R=$?
if [ $R -eq 0 ]; then
echo -n "$prog is already running. exiting."
echoFailure
return 1
fi
touch ${CATALINA_PID}
chown ${RUNAS_USER}:${RUNAS_USER} ${CATALINA_PID}
cd ${CATALINA_HOME}
runuser -s /bin/bash ${RUNAS_USER} -c "${RUN_SCRIPT} start >>${CATALINA_HOME}/logs/catalina.out 2>&1"
RETVAL=$?
cd ${CDIR}
if [ $RETVAL -eq 0 ]; then
echoSuccess
else
echoFailure
fi
}
status() {
RETVAL=1
if [ -s ${CATALINA_PID} ]; then
read kpid < ${CATALINA_PID}
if checkpid $kpid 2>&1; then
echo "$prog is running ($kpid)"
RETVAL=0
else
echo "The lock file was found but no process running for pid $kpid."
fi
else
CHECKPROC="Dcatalina.home=${CATALINA_HOME}.*org.apache.catalina.startup.Bootstrap"
pid="$(pgrep -u ${RUNAS_USER} -f ${CHECKPROC})"
if [ -n "$pid" ]; then
echo "$prog is running ($pid) but no PID file exists (or it's empty)."
RETVAL="0"
else
echo "$prog is stopped."
fi
fi
return $RETVAL
}
stop() {
echo -n "Stopping $prog."
if status >/dev/null; then
runuser -s /bin/bash ${RUNAS_USER} -c "${RUN_SCRIPT} stop >>${CATALINA_HOME}/logs/catalina.out 2>&1"
COUNT=0
while [ $COUNT -lt ${WAITFOR} ]; do
if status >/dev/null; then
echo -n "."
sleep 1
else
echoSuccess
cleanup
return
fi
COUNT=$(expr $COUNT + 1)
done
echo
echo -n "$prog didn't stop, killing it softly.."
CHECKPROC="Dcatalina.home=${CATALINA_HOME}.*org.apache.catalina.startup.Bootstrap"
pid="$(pgrep -u ${RUNAS_USER} -f ${CHECKPROC})"
kill $pid
sleep 4
if status >/dev/null; then
echo
echo "Shutdown is taking too long, force stopping $prog."
kill -9 $pid
if status >/dev/null; then
echoFailure
else
echoSuccess
cleanup
return
fi
else
echoSuccess
cleanup
return
fi
else
echo
echo -n "$prog is not running. Exiting."
echoFailure
fi
}
case "$1" in
start) start;;
stop) stop;;
status) status;;
restart) stop ; start;;
*) echo "Usage: $0 [ start | stop | status | restart ]";;
esac