4 # Copyright (c) 1999, 2008 Tanuki Software, Inc.
5 # http://www.tanukisoftware.com
8 # This software is the confidential and proprietary information
9 # of Tanuki Software. ("Confidential Information"). You shall
10 # not disclose such Confidential Information and shall use it
11 # only in accordance with the terms of the license agreement you
12 # entered into with Tanuki Software.
14 # Java Service Wrapper sh script. Suitable for starting and stopping
15 # wrapped Java applications on UNIX platforms.
17 export SONAR_HOME
=${SONAR_HOME:=/opt/sonar}
18 export SONAR_RUN_DIR
=${SONAR_RUN_DIR:=/var/run/sonar}
19 export SONAR_ETC_DIR
=${SONAR_ETC_DIR:=/etc/sonar}
20 export SONAR_LOG_DIR
=${SONAR_LOG_DIR:=/var/log/sonar}
22 #-----------------------------------------------------------------------------
23 # These settings can be modified to fit the needs of your application
24 # Optimized for use with version 3.3.0 of the Wrapper.
26 # These settings are highly modified for the use of the sonar application
34 WRAPPER_CMD
="$SONAR_HOME/bin/wrapper"
35 WRAPPER_CONF
="$SONAR_ETC_DIR/wrapper.conf"
37 # Priority at which to run the wrapper. See "man nice" for valid priorities.
38 # nice is only used if a priority is specified.
41 # Location of the pid file.
42 PIDDIR
="$SONAR_RUN_DIR"
44 # If uncommented, causes the Wrapper to be shutdown using an anchor file.
45 # When launched with the 'start' command, it will also ignore all INT and
49 # Wrapper will start the JVM asynchronously. Your application may have some
50 # initialization tasks and it may be desirable to wait a few seconds
51 # before returning. For example, to delay the invocation of following
52 # startup scripts. Setting WAIT_AFTER_STARTUP to a positive number will
53 # cause the start command to delay for the indicated period of time
58 # If set, the status, start_msg and stop_msg commands will print out detailed
59 # state information on the Wrapper and Java processes.
62 # If specified, the Wrapper will be run as the specified user.
63 # IMPORTANT - Make sure that the user has the required privileges to write
64 # the PID file and wrapper.log files. Failure to be able to write the log
65 # file will cause the Wrapper to exit without any way to write out an error
67 # NOTE - This will set the user which is used to run the Wrapper as well as
68 # the JVM and is not useful in situations where a privileged resource or
69 # port needs to be allocated prior to the user being changed.
72 # The following two lines are used by the chkconfig command. Change as is
73 # appropriate for your application. They should remain commented.
74 # chkconfig: 2345 20 80
75 # description: "Sonar Server"
77 # Initialization block for the install_initd and remove_initd scripts used by
78 # SUSE linux distributions.
80 # Provides: @app.name@
81 # Required-Start: $local_fs $network $syslog
84 # Default-Start: 2 3 4 5
86 # Short-Description: @app.long.name@
87 # Description: @app.description@
90 # Do not modify anything beyond this point
91 #-----------------------------------------------------------------------------
93 # Get the fully qualified path to the script
104 # Resolve the true real path without any sym links.
106 while [ "X$CHANGED" != "X" ]
108 # Change spaces to ":" so the tokens can be parsed.
109 SAFESCRIPT
=`echo $SCRIPT | sed -e 's; ;:;g'`
110 # Get the real path to this script, resolving any symbolic links
111 TOKENS
=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
114 # Change any ":" in the token back to a space.
115 C
=`echo $C | sed -e 's;:; ;g'`
116 REALPATH
="$REALPATH/$C"
117 # If REALPATH is a sym link, resolve it. Loop for nested links.
118 while [ -h "$REALPATH" ] ; do
119 LS
="`ls -ld "$REALPATH"`"
120 LINK
="`expr "$LS" : '.*-> \(.*\)$'`"
121 if expr "$LINK" : '/.*' > /dev
/null
; then
126 REALPATH
="`dirname "$REALPATH"`""/$LINK"
131 if [ "$REALPATH" = "$SCRIPT" ]
139 # Change the current directory to the location of the script
140 cd "`dirname "$REALPATH"`"
143 # If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
144 # the working directory is later changed.
145 FIRST_CHAR
=`echo $PIDDIR | cut -c1,1`
146 if [ "$FIRST_CHAR" != "/" ]
148 PIDDIR
=$REALDIR/$PIDDIR
150 # Same test for WRAPPER_CMD
151 FIRST_CHAR
=`echo $WRAPPER_CMD | cut -c1,1`
152 if [ "$FIRST_CHAR" != "/" ]
154 WRAPPER_CMD
=$REALDIR/$WRAPPER_CMD
156 # Same test for WRAPPER_CONF
157 FIRST_CHAR
=`echo $WRAPPER_CONF | cut -c1,1`
158 if [ "$FIRST_CHAR" != "/" ]
160 WRAPPER_CONF
=$REALDIR/$WRAPPER_CONF
164 ANCHORFILE
="$PIDDIR/$APP_NAME.anchor"
165 STATUSFILE
="$PIDDIR/$APP_NAME.status"
166 JAVASTATUSFILE
="$PIDDIR/$APP_NAME.java.status"
167 PIDFILE
="$PIDDIR/$APP_NAME.pid"
168 LOCKDIR
="/var/lock/subsys"
169 LOCKFILE
="$LOCKDIR/$APP_NAME"
172 # Resolve the location of the 'ps' command
179 echo "Unable to locate 'ps'."
180 echo "Please report this message along with the location of the command on your system."
186 DIST_OS
=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
192 # HP-UX needs the XPG4 version of ps (for -o args)
205 # Resolve the architecture
206 if [ "$DIST_OS" = "macosx" ]
208 DIST_ARCH
="universal"
211 DIST_ARCH
=`uname -p 2>/dev/null | tr [:upper:] [:lower:] | tr -d [:blank:]`
212 if [ "X$DIST_ARCH" = "X" ]
216 if [ "$DIST_ARCH" = "unknown" ]
218 DIST_ARCH
=`uname -m 2>/dev/null | tr [:upper:] [:lower:] | tr -d [:blank:]`
221 'amd64' |
'athlon' |
'i386' |
'i486' |
'i586' |
'i686' |
'x86_64')
224 'ia32' |
'ia64' |
'ia64n' |
'ia64w')
230 'power' |
'powerpc' |
'power_pc' |
'ppc64')
233 'pa_risc' |
'pa-risc')
245 # OSX always places Java in the same location so we can reliably set JAVA_HOME
246 if [ "$DIST_OS" = "macosx" ]
248 if [ -z "$JAVA_HOME" ]; then
249 JAVA_HOME
="/Library/Java/Home"; export JAVA_HOME
256 echo " $1 (Found but not executable.)";
262 # Decide on the wrapper binary to use.
263 # If a 32-bit wrapper binary exists then it will work on 32 or 64 bit
264 # platforms, if the 64-bit binary exists then the distribution most
265 # likely wants to use long names. Otherwise, look for the default.
266 WRAPPER_TEST_CMD
="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
267 if [ -x "$WRAPPER_TEST_CMD" ]
269 WRAPPER_CMD
="$WRAPPER_TEST_CMD"
271 WRAPPER_TEST_CMD
="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
272 if [ -x "$WRAPPER_TEST_CMD" ]
274 WRAPPER_CMD
="$WRAPPER_TEST_CMD"
276 if [ ! -x "$WRAPPER_CMD" ]
278 echo "Unable to locate any of the following binaries:"
279 outputFile
"$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
280 outputFile
"$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
281 outputFile
"$WRAPPER_CMD"
287 # Build the nice clause
288 if [ "X$PRIORITY" = "X" ]
292 CMDNICE
="nice -$PRIORITY"
295 # Build the anchor file clause.
296 if [ "X$IGNORE_SIGNALS" = "X" ]
301 ANCHORPROP
=wrapper.anchorfile
=\"$ANCHORFILE\"
302 IGNOREPROP
=wrapper.ignore_signals
=TRUE
305 # Build the status file clause.
306 if [ "X$DETAIL_STATUS" = "X" ]
310 STATUSPROP
="wrapper.statusfile=\"$STATUSFILE\" wrapper.java.statusfile=\"$JAVASTATUSFILE\""
313 # Build the lock file clause. Only create a lock file if the lock directory exists on this platform.
319 LOCKPROP
=wrapper.lockfile
=\"$LOCKFILE\"
327 # Check the configured user. If necessary rerun this script as the desired user.
328 if [ "X$RUN_AS_USER" != "X" ]
330 # Resolve the location of the 'id' command
331 IDEXE
="/usr/xpg4/bin/id"
337 echo "Unable to locate 'id'."
338 echo "Please report this message along with the location of the command on your system."
343 if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
345 # Already running as the configured user. Avoid password prompts by not calling su.
349 if [ "X$RUN_AS_USER" != "X" ]
351 # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
352 # able to create the lock file. The Wrapper will be able to update this file once it
353 # is created but will not be able to delete it on shutdown. If $2 is defined then
354 # the lock file should be created for the current command
355 if [ "X$LOCKPROP" != "X" ]
359 # Resolve the primary group
360 RUN_AS_GROUP
=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
361 if [ "X$RUN_AS_GROUP" = "X" ]
363 RUN_AS_GROUP
=$RUN_AS_USER
366 chown
$RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
370 # Still want to change users, recurse. This means that the user will only be
371 # prompted for a password once. Variables shifted by 1
373 # Use "runuser" if this exists. runuser should be used on RedHat in preference to su.
375 if test -f "/sbin/runuser"
377 /sbin
/runuser
- $RUN_AS_USER -c "\"$REALPATH\" $2"
379 su
- $RUN_AS_USER -c "\"$REALPATH\" $2"
382 # Now that we are the original user again, we may need to clean up the lock file.
383 if [ "X$LOCKPROP" != "X" ]
388 # Wrapper is not running so make sure the lock file is deleted.
389 if [ -f "$LOCKFILE" ]
407 if [ "X$pid" != "X" ]
409 # It is possible that 'a' process with the pid exists but that it is not the
410 # correct process. This can happen in a number of cases, but the most
411 # common is during system startup after an unclean shutdown.
412 # The ps statement below looks for the specific wrapper command running as
413 # the pid. If it is not found then the pid file is considered to be stale.
416 pidtest
=`$PSEXE -ww -p $pid -o command | grep "$WRAPPER_CMD" | tail -1`
419 pidtest
=`$PSEXE -p $pid -o args | grep "$WRAPPER_CMD" | tail -1`
423 if [ "X$pidtest" = "X" ]
425 # This is a stale pid file.
427 echo "Removed stale pid file: $PIDFILE"
432 echo "Cannot read $PIDFILE."
440 if [ -f "$STATUSFILE" ]
442 if [ -r "$STATUSFILE" ]
444 STATUS
=`cat "$STATUSFILE"`
447 if [ "X$STATUS" = "X" ]
453 if [ -f "$JAVASTATUSFILE" ]
455 if [ -r "$JAVASTATUSFILE" ]
457 JAVASTATUS
=`cat "$JAVASTATUSFILE"`
460 if [ "X$JAVASTATUS" = "X" ]
467 pid
=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
470 # Process is gone so remove the pid file.
477 echo "Running $APP_LONG_NAME..."
481 # The string passed to eval must handles spaces in paths correctly.
482 COMMAND_LINE
="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" wrapper.name=\"$APP_NAME\" wrapper.displayname=\"$APP_LONG_NAME\" $ANCHORPROP $STATUSPROP $LOCKPROP"
485 echo "$APP_LONG_NAME is already running."
491 echo -n "Starting $APP_LONG_NAME..."
495 # The string passed to eval must handles spaces in paths correctly.
496 COMMAND_LINE
="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" wrapper.name=\"$APP_NAME\" wrapper.displayname=\"$APP_LONG_NAME\" wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $STATUSPROP $LOCKPROP"
499 echo "$APP_LONG_NAME is already running."
503 # Sleep for a few seconds to allow for intialization if required
504 # then test to make sure we're still running.
507 while [ $i -lt $WAIT_AFTER_STARTUP ]
513 if [ $WAIT_AFTER_STARTUP -gt 0 ]
518 echo " WARNING: $APP_LONG_NAME may have failed to start."
521 echo " running ($pid)."
529 echo "Stopping $APP_LONG_NAME..."
533 echo "$APP_LONG_NAME was not running."
535 if [ "X$IGNORE_SIGNALS" = "X" ]
537 # Running so try to stop it.
541 # An explanation for the failure should have been given
542 echo "Unable to stop $APP_LONG_NAME."
547 if [ -f "$ANCHORFILE" ]
549 # An explanation for the failure should have been given
550 echo "Unable to stop $APP_LONG_NAME."
555 # We can not predict how long it will take for the wrapper to
556 # actually stop as it depends on settings in wrapper.conf.
557 # Loop until it does.
561 while [ "X$pid" != "X" ]
563 # Show a waiting message every 5 seconds.
564 if [ "$CNT" -lt "5" ]
568 echo "Waiting for $APP_LONG_NAME to exit..."
571 TOTCNT
=`expr $TOTCNT + 1`
580 if [ "X$pid" != "X" ]
582 echo "Failed to stop $APP_LONG_NAME."
585 echo "Stopped $APP_LONG_NAME."
594 echo "$APP_LONG_NAME is not running."
597 if [ "X$DETAIL_STATUS" = "X" ]
599 echo "$APP_LONG_NAME is running (PID:$pid)."
602 echo "$APP_LONG_NAME is running (PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS)"
609 echo "Dumping $APP_LONG_NAME..."
613 echo "$APP_LONG_NAME was not running."
619 echo "Failed to dump $APP_LONG_NAME."
622 echo "Dumped $APP_LONG_NAME."
627 # Used by HP-UX init scripts.
632 echo "Starting $APP_LONG_NAME... (Wrapper:Stopped)"
634 if [ "X$DETAIL_STATUS" = "X" ]
636 echo "Starting $APP_LONG_NAME... (Wrapper:Running)"
639 echo "Starting $APP_LONG_NAME... (Wrapper:$STATUS, Java:$JAVASTATUS)"
644 # Used by HP-UX init scripts.
649 echo "Stopping $APP_LONG_NAME... (Wrapper:Stopped)"
651 if [ "X$DETAIL_STATUS" = "X" ]
653 echo "Stopping $APP_LONG_NAME... (Wrapper:Running)"
656 echo "Stopping $APP_LONG_NAME... (Wrapper:$STATUS, Java:$JAVASTATUS)"
664 checkUser touchlock
$1
669 checkUser touchlock
$1
679 checkUser touchlock
$1
705 echo "Usage: $0 { console | start | stop | restart | status | dump }"