~upstart-devel/upstart/upstart-jobs

« back to all changes in this revision

Viewing changes to etc/init.d/tomcat7

  • Committer: Dimitri John Ledkov
  • Date: 2014-05-06 18:45:46 UTC
  • Revision ID: dimitri.ledkov@canonical.com-20140506184546-5toyx56xxrue0f0v
auto update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
#
3
 
# /etc/init.d/tomcat7 -- startup script for the Tomcat 6 servlet engine
4
 
#
5
 
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
6
 
# Modified for Debian GNU/Linux by Ian Murdock <imurdock@gnu.ai.mit.edu>.
7
 
# Modified for Tomcat by Stefan Gybas <sgybas@debian.org>.
8
 
# Modified for Tomcat6 by Thierry Carrez <thierry.carrez@ubuntu.com>.
9
 
# Modified for Tomcat7 by Ernesto Hernandez-Novich <emhn@itverx.com.ve>.
10
 
# Additional improvements by Jason Brittain <jason.brittain@mulesoft.com>.
11
 
#
12
 
### BEGIN INIT INFO
13
 
# Provides:          tomcat7
14
 
# Required-Start:    $local_fs $remote_fs $network
15
 
# Required-Stop:     $local_fs $remote_fs $network
16
 
# Should-Start:      $named
17
 
# Should-Stop:       $named
18
 
# Default-Start:     2 3 4 5
19
 
# Default-Stop:      0 1 6
20
 
# Short-Description: Start Tomcat.
21
 
# Description:       Start the Tomcat servlet engine.
22
 
### END INIT INFO
23
 
 
24
 
set -e
25
 
 
26
 
PATH=/bin:/usr/bin:/sbin:/usr/sbin
27
 
NAME=tomcat7
28
 
DESC="Tomcat servlet engine"
29
 
DEFAULT=/etc/default/$NAME
30
 
JVM_TMP=/tmp/tomcat7-$NAME-tmp
31
 
 
32
 
if [ `id -u` -ne 0 ]; then
33
 
        echo "You need root privileges to run this script"
34
 
        exit 1
35
 
fi
36
 
 
37
 
# Make sure tomcat is started with system locale
38
 
if [ -r /etc/default/locale ]; then
39
 
        . /etc/default/locale
40
 
        export LANG
41
 
fi
42
 
 
43
 
. /lib/lsb/init-functions
44
 
 
45
 
if [ -r /etc/default/rcS ]; then
46
 
        . /etc/default/rcS
47
 
fi
48
 
 
49
 
 
50
 
# The following variables can be overwritten in $DEFAULT
51
 
 
52
 
# Run Tomcat 7 as this user ID and group ID
53
 
TOMCAT7_USER=tomcat7
54
 
TOMCAT7_GROUP=tomcat7
55
 
 
56
 
# this is a work-around until there is a suitable runtime replacement 
57
 
# for dpkg-architecture for arch:all packages
58
 
# this function sets the variable OPENJDKS
59
 
find_openjdks()
60
 
{
61
 
        for jvmdir in /usr/lib/jvm/java-7-openjdk-*
62
 
        do
63
 
                if [ -d "${jvmdir}" -a "${jvmdir}" != "/usr/lib/jvm/java-7-openjdk-common" ]
64
 
                then
65
 
                        OPENJDKS=$jvmdir
66
 
                fi
67
 
        done
68
 
        for jvmdir in /usr/lib/jvm/java-6-openjdk-*
69
 
        do
70
 
                if [ -d "${jvmdir}" -a "${jvmdir}" != "/usr/lib/jvm/java-6-openjdk-common" ]
71
 
                then
72
 
                        OPENJDKS="${OPENJDKS} ${jvmdir}"
73
 
                fi
74
 
        done
75
 
}
76
 
 
77
 
OPENJDKS=""
78
 
find_openjdks
79
 
# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
80
 
# defined in $DEFAULT)
81
 
JDK_DIRS="/usr/lib/jvm/default-java ${OPENJDKS} /usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-sun /usr/lib/jvm/java-7-oracle"
82
 
 
83
 
# Look for the right JVM to use
84
 
for jdir in $JDK_DIRS; do
85
 
    if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
86
 
        JAVA_HOME="$jdir"
87
 
    fi
88
 
done
89
 
export JAVA_HOME
90
 
 
91
 
# Directory where the Tomcat 6 binary distribution resides
92
 
CATALINA_HOME=/usr/share/$NAME
93
 
 
94
 
# Directory for per-instance configuration files and webapps
95
 
CATALINA_BASE=/var/lib/$NAME
96
 
 
97
 
# Use the Java security manager? (yes/no)
98
 
TOMCAT7_SECURITY=no
99
 
 
100
 
# Default Java options
101
 
# Set java.awt.headless=true if JAVA_OPTS is not set so the
102
 
# Xalan XSL transformer can work without X11 display on JDK 1.4+
103
 
# It also looks like the default heap size of 64M is not enough for most cases
104
 
# so the maximum heap size is set to 128M
105
 
if [ -z "$JAVA_OPTS" ]; then
106
 
        JAVA_OPTS="-Djava.awt.headless=true -Xmx128M"
107
 
fi
108
 
 
109
 
# End of variables that can be overwritten in $DEFAULT
110
 
 
111
 
# overwrite settings from default file
112
 
if [ -f "$DEFAULT" ]; then
113
 
        . "$DEFAULT"
114
 
fi
115
 
 
116
 
if [ ! -f "$CATALINA_HOME/bin/bootstrap.jar" ]; then
117
 
        log_failure_msg "$NAME is not installed"
118
 
        exit 1
119
 
fi
120
 
 
121
 
POLICY_CACHE="$CATALINA_BASE/work/catalina.policy"
122
 
 
123
 
if [ -z "$CATALINA_TMPDIR" ]; then
124
 
        CATALINA_TMPDIR="$JVM_TMP"
125
 
fi
126
 
 
127
 
# Set the JSP compiler if set in the tomcat7.default file
128
 
if [ -n "$JSP_COMPILER" ]; then
129
 
        JAVA_OPTS="$JAVA_OPTS -Dbuild.compiler=\"$JSP_COMPILER\""
130
 
fi
131
 
 
132
 
SECURITY=""
133
 
if [ "$TOMCAT7_SECURITY" = "yes" ]; then
134
 
        SECURITY="-security"
135
 
fi
136
 
 
137
 
# Define other required variables
138
 
CATALINA_PID="/var/run/$NAME.pid"
139
 
CATALINA_SH="$CATALINA_HOME/bin/catalina.sh"
140
 
 
141
 
# Look for Java Secure Sockets Extension (JSSE) JARs
142
 
if [ -z "${JSSE_HOME}" -a -r "${JAVA_HOME}/jre/lib/jsse.jar" ]; then
143
 
    JSSE_HOME="${JAVA_HOME}/jre/"
144
 
fi
145
 
 
146
 
catalina_sh() {
147
 
        # Escape any double quotes in the value of JAVA_OPTS
148
 
        JAVA_OPTS="$(echo $JAVA_OPTS | sed 's/\"/\\\"/g')"
149
 
 
150
 
        AUTHBIND_COMMAND=""
151
 
        if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then
152
 
                AUTHBIND_COMMAND="/usr/bin/authbind --deep /bin/bash -c "
153
 
        fi
154
 
 
155
 
        # Define the command to run Tomcat's catalina.sh as a daemon
156
 
        # set -a tells sh to export assigned variables to spawned shells.
157
 
        TOMCAT_SH="set -a; JAVA_HOME=\"$JAVA_HOME\"; source \"$DEFAULT\"; \
158
 
                CATALINA_HOME=\"$CATALINA_HOME\"; \
159
 
                CATALINA_BASE=\"$CATALINA_BASE\"; \
160
 
                JAVA_OPTS=\"$JAVA_OPTS\"; \
161
 
                CATALINA_PID=\"$CATALINA_PID\"; \
162
 
                CATALINA_TMPDIR=\"$CATALINA_TMPDIR\"; \
163
 
                LANG=\"$LANG\"; JSSE_HOME=\"$JSSE_HOME\"; \
164
 
                cd \"$CATALINA_BASE\"; \
165
 
                \"$CATALINA_SH\" $@"
166
 
 
167
 
        if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; then
168
 
                TOMCAT_SH="'$TOMCAT_SH'"
169
 
        fi
170
 
 
171
 
        # Run the catalina.sh script as a daemon
172
 
        set +e
173
 
        touch "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out
174
 
        chown $TOMCAT7_USER "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.out
175
 
        start-stop-daemon --start -b -u "$TOMCAT7_USER" -g "$TOMCAT7_GROUP" \
176
 
                -c "$TOMCAT7_USER" -d "$CATALINA_TMPDIR" -p "$CATALINA_PID" \
177
 
                -x /bin/bash -- -c "$AUTHBIND_COMMAND $TOMCAT_SH"
178
 
        status="$?"
179
 
        set +a -e
180
 
        return $status
181
 
}
182
 
 
183
 
case "$1" in
184
 
  start)
185
 
        if [ -z "$JAVA_HOME" ]; then
186
 
                log_failure_msg "no JDK or JRE found - please set JAVA_HOME"
187
 
                exit 1
188
 
        fi
189
 
 
190
 
        if [ ! -d "$CATALINA_BASE/conf" ]; then
191
 
                log_failure_msg "invalid CATALINA_BASE: $CATALINA_BASE"
192
 
                exit 1
193
 
        fi
194
 
 
195
 
        log_daemon_msg "Starting $DESC" "$NAME"
196
 
        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
197
 
                --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
198
 
                >/dev/null; then
199
 
 
200
 
                # Regenerate POLICY_CACHE file
201
 
                umask 022
202
 
                echo "// AUTO-GENERATED FILE from /etc/tomcat7/policy.d/" \
203
 
                        > "$POLICY_CACHE"
204
 
                echo ""  >> "$POLICY_CACHE"
205
 
                cat $CATALINA_BASE/conf/policy.d/*.policy \
206
 
                        >> "$POLICY_CACHE"
207
 
 
208
 
                # Remove / recreate JVM_TMP directory
209
 
                rm -rf "$JVM_TMP"
210
 
                mkdir -p "$JVM_TMP" || {
211
 
                        log_failure_msg "could not create JVM temporary directory"
212
 
                        exit 1
213
 
                }
214
 
                chown $TOMCAT7_USER "$JVM_TMP"
215
 
 
216
 
                catalina_sh start $SECURITY
217
 
                sleep 5
218
 
                if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
219
 
                        --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
220
 
                        >/dev/null; then
221
 
                        if [ -f "$CATALINA_PID" ]; then
222
 
                                rm -f "$CATALINA_PID"
223
 
                        fi
224
 
                        log_end_msg 1
225
 
                else
226
 
                        log_end_msg 0
227
 
                fi
228
 
        else
229
 
                log_progress_msg "(already running)"
230
 
                log_end_msg 0
231
 
        fi
232
 
        ;;
233
 
  stop)
234
 
        log_daemon_msg "Stopping $DESC" "$NAME"
235
 
 
236
 
        set +e
237
 
        if [ -f "$CATALINA_PID" ]; then 
238
 
                start-stop-daemon --stop --pidfile "$CATALINA_PID" \
239
 
                        --user "$TOMCAT7_USER" \
240
 
                        --retry=TERM/20/KILL/5 >/dev/null
241
 
                if [ $? -eq 1 ]; then
242
 
                        log_progress_msg "$DESC is not running but pid file exists, cleaning up"
243
 
                elif [ $? -eq 3 ]; then
244
 
                        PID="`cat $CATALINA_PID`"
245
 
                        log_failure_msg "Failed to stop $NAME (pid $PID)"
246
 
                        exit 1
247
 
                fi
248
 
                rm -f "$CATALINA_PID"
249
 
                rm -rf "$JVM_TMP"
250
 
        else
251
 
                log_progress_msg "(not running)"
252
 
        fi
253
 
        log_end_msg 0
254
 
        set -e
255
 
        ;;
256
 
   status)
257
 
        set +e
258
 
        start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
259
 
                --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
260
 
                >/dev/null 2>&1
261
 
        if [ "$?" = "0" ]; then
262
 
 
263
 
                if [ -f "$CATALINA_PID" ]; then
264
 
                    log_success_msg "$DESC is not running, but pid file exists."
265
 
                        exit 1
266
 
                else
267
 
                    log_success_msg "$DESC is not running."
268
 
                        exit 3
269
 
                fi
270
 
        else
271
 
                log_success_msg "$DESC is running with pid `cat $CATALINA_PID`"
272
 
        fi
273
 
        set -e
274
 
        ;;
275
 
  restart|force-reload)
276
 
        if [ -f "$CATALINA_PID" ]; then
277
 
                $0 stop
278
 
                sleep 1
279
 
        fi
280
 
        $0 start
281
 
        ;;
282
 
  try-restart)
283
 
        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
284
 
                --user $TOMCAT7_USER --exec "$JAVA_HOME/bin/java" \
285
 
                >/dev/null; then
286
 
                $0 start
287
 
        fi
288
 
        ;;
289
 
  *)
290
 
        log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
291
 
        exit 1
292
 
        ;;
293
 
esac
294
 
 
295
 
exit 0