4 # Copyright (c) 1999, 2006 Tanuki Software Inc.
6 # Java Service Wrapper sh script. Suitable for starting and stopping
7 # wrapped Java applications on UNIX platforms.
10 #-----------------------------------------------------------------------------
11 # These settings can be modified to fit the needs of your application
13 # Default values for the Application variables, below.
15 # NOTE: The build for specific applications may override this during the resource-copying
16 # phase, to fill in a concrete name and avoid the use of the defaults specified here.
17 DEF_APP_NAME
="testwrapper"
18 DEF_APP_LONG_NAME
="Test Wrapper Sample Application"
21 APP_NAME
="nexus-webapp"
22 APP_LONG_NAME
="Sonatype Nexus Repository Manager"
25 WRAPPER_CMD
="/opt/nexus/bin/wrapper"
26 WRAPPER_CONF
="/opt/nexus/conf/wrapper.conf"
28 # Priority at which to run the wrapper. See "man nice" for valid priorities.
29 # nice is only used if a priority is specified.
32 # Location of the pid file.
35 # If uncommented, causes the Wrapper to be shutdown using an anchor file.
36 # When launched with the 'start' command, it will also ignore all INT and
40 # If specified, the Wrapper will be run as the specified user.
41 # IMPORTANT - Make sure that the user has the required privileges to write
42 # the PID file and wrapper.log files. Failure to be able to write the log
43 # file will cause the Wrapper to exit without any way to write out an error
45 # NOTE - This will set the user which is used to run the Wrapper as well as
46 # the JVM and is not useful in situations where a privileged resource or
47 # port needs to be allocated prior to the user being changed.
50 # The following two lines are used by the chkconfig command. Change as is
51 # appropriate for your application. They should remain commented.
52 # chkconfig: 2345 20 80
53 # description: Test Wrapper Sample Application
55 # Do not modify anything beyond this point
56 #-----------------------------------------------------------------------------
58 # Get the fully qualified path to the script
69 # Resolve the true real path without any sym links.
71 while [ "X$CHANGED" != "X" ]
73 # Change spaces to ":" so the tokens can be parsed.
74 SAFESCRIPT
=`echo $SCRIPT | sed -e 's; ;:;g'`
75 # Get the real path to this script, resolving any symbolic links
76 TOKENS
=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
79 # Change any ":" in the token back to a space.
80 C
=`echo $C | sed -e 's;:; ;g'`
81 REALPATH
="$REALPATH/$C"
82 # If REALPATH is a sym link, resolve it. Loop for nested links.
83 while [ -h "$REALPATH" ] ; do
84 LS
="`ls -ld "$REALPATH"`"
85 LINK
="`expr "$LS" : '.*-> \(.*\)$'`"
86 if expr "$LINK" : '/.*' > /dev
/null
; then
91 REALPATH
="`dirname "$REALPATH"`""/$LINK"
96 if [ "$REALPATH" = "$SCRIPT" ]
104 # Change the current directory to the location of the script
105 cd "`dirname "$REALPATH"`"
108 # If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
109 # the working directory is later changed.
110 FIRST_CHAR
=`echo $PIDDIR | cut -c1,1`
111 if [ "$FIRST_CHAR" != "/" ]
113 PIDDIR
=$REALDIR/$PIDDIR
115 # Same test for WRAPPER_CMD
116 FIRST_CHAR
=`echo $WRAPPER_CMD | cut -c1,1`
117 if [ "$FIRST_CHAR" != "/" ]
119 WRAPPER_CMD
=$REALDIR/$WRAPPER_CMD
121 # Same test for WRAPPER_CONF
122 FIRST_CHAR
=`echo $WRAPPER_CONF | cut -c1,1`
123 if [ "$FIRST_CHAR" != "/" ]
125 WRAPPER_CONF
=$REALDIR/$WRAPPER_CONF
129 ANCHORFILE
="$PIDDIR/$APP_NAME.anchor"
130 PIDFILE
="$PIDDIR/$APP_NAME.pid"
131 LOCKDIR
="/var/lock/subsys"
132 LOCKFILE
="$LOCKDIR/$APP_NAME"
135 # Resolve the location of the 'ps' command
142 echo "Unable to locate 'ps'."
143 echo "Please report this message along with the location of the command on your system."
149 DIST_OS
=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
165 # Resolve the architecture
166 DIST_ARCH
=`uname -p | tr [:upper:] [:lower:] | tr -d [:blank:]`
167 if [ "$DIST_ARCH" = "unknown" ]
169 DIST_ARCH
=`uname -m | tr [:upper:] [:lower:] | tr -d [:blank:]`
172 'amd64' |
'athlon' |
'ia32' |
'ia64' |
'i386' |
'i486' |
'i586' |
'i686' |
'x86_64')
178 'power' |
'powerpc' |
'power_pc' |
'ppc64')
181 'pa_risc' |
'pa-risc')
195 echo " $1 (Found but not executable.)";
201 # Decide on the wrapper binary to use.
202 # If a 32-bit wrapper binary exists then it will work on 32 or 64 bit
203 # platforms, if the 64-bit binary exists then the distribution most
204 # likely wants to use long names. Otherwise, look for the default.
205 # For macosx, we also want to look for universal binaries.
206 WRAPPER_TEST_CMD
="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
207 if [ -x "$WRAPPER_TEST_CMD" ]
209 WRAPPER_CMD
="$WRAPPER_TEST_CMD"
211 if [ "$DIST_OS" = "macosx" ]
213 WRAPPER_TEST_CMD
="$WRAPPER_CMD-$DIST_OS-universal-32"
214 if [ -x "$WRAPPER_TEST_CMD" ]
216 WRAPPER_CMD
="$WRAPPER_TEST_CMD"
218 WRAPPER_TEST_CMD
="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
219 if [ -x "$WRAPPER_TEST_CMD" ]
221 WRAPPER_CMD
="$WRAPPER_TEST_CMD"
223 WRAPPER_TEST_CMD
="$WRAPPER_CMD-$DIST_OS-universal-64"
224 if [ -x "$WRAPPER_TEST_CMD" ]
226 WRAPPER_CMD
="$WRAPPER_TEST_CMD"
228 if [ ! -x "$WRAPPER_CMD" ]
230 echo "Unable to locate any of the following binaries:"
231 outputFile
"$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
232 outputFile
"$WRAPPER_CMD-$DIST_OS-universal-32"
233 outputFile
"$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
234 outputFile
"$WRAPPER_CMD-$DIST_OS-universal-64"
235 outputFile
"$WRAPPER_CMD"
242 WRAPPER_TEST_CMD
="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
243 if [ -x "$WRAPPER_TEST_CMD" ]
245 WRAPPER_CMD
="$WRAPPER_TEST_CMD"
247 if [ ! -x "$WRAPPER_CMD" ]
249 echo "Unable to locate any of the following binaries:"
250 outputFile
"$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
251 outputFile
"$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
252 outputFile
"$WRAPPER_CMD"
259 # Build the nice clause
260 if [ "X$PRIORITY" = "X" ]
264 CMDNICE
="nice -$PRIORITY"
267 # Build the anchor file clause.
268 if [ "X$IGNORE_SIGNALS" = "X" ]
273 ANCHORPROP
=wrapper.anchorfile
=\"$ANCHORFILE\"
274 IGNOREPROP
=wrapper.ignore_signals
=TRUE
277 # Build the lock file clause. Only create a lock file if the lock directory exists on this platform.
283 LOCKPROP
=wrapper.lockfile
=\"$LOCKFILE\"
291 # Check the configured user. If necessary rerun this script as the desired user.
292 if [ "X$RUN_AS_USER" != "X" ]
294 # Resolve the location of the 'id' command
295 IDEXE
="/usr/xpg4/bin/id"
301 echo "Unable to locate 'id'."
302 echo "Please report this message along with the location of the command on your system."
307 if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
309 # Already running as the configured user. Avoid password prompts by not calling su.
313 if [ "X$RUN_AS_USER" != "X" ]
315 # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
316 # able to create the lock file. The Wrapper will be able to update this file once it
317 # is created but will not be able to delete it on shutdown. If $2 is defined then
318 # the lock file should be created for the current command
319 if [ "X$LOCKPROP" != "X" ]
323 # Resolve the primary group
324 RUN_AS_GROUP
=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
325 if [ "X$RUN_AS_GROUP" = "X" ]
327 RUN_AS_GROUP
=$RUN_AS_USER
330 chown
$RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
334 # Still want to change users, recurse. This means that the user will only be
335 # prompted for a password once. Variables shifted by 1
336 su
-m $RUN_AS_USER -c "\"$REALPATH\" $2"
338 # Now that we are the original user again, we may need to clean up the lock file.
339 if [ "X$LOCKPROP" != "X" ]
344 # Wrapper is not running so make sure the lock file is deleted.
345 if [ -f "$LOCKFILE" ]
362 if [ "X$pid" != "X" ]
364 # It is possible that 'a' process with the pid exists but that it is not the
365 # correct process. This can happen in a number of cases, but the most
366 # common is during system startup after an unclean shutdown.
367 # The ps statement below looks for the specific wrapper command running as
368 # the pid. If it is not found then the pid file is considered to be stale.
369 pidtest
=`$PSEXE -p $pid -o args | grep "$WRAPPER_CMD" | tail -1`
370 if [ "X$pidtest" = "X" ]
372 # This is a stale pid file.
374 echo "Removed stale pid file: $PIDFILE"
379 echo "Cannot read $PIDFILE."
386 pid
=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
389 # Process is gone so remove the pid file.
396 echo "Running $APP_LONG_NAME..."
400 # The string passed to eval must handles spaces in paths correctly.
401 COMMAND_LINE
="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=$APP_NAME wrapper.pidfile=\"$PIDFILE\" $ANCHORPROP $LOCKPROP"
404 echo "$APP_LONG_NAME is already running."
410 echo "Starting $APP_LONG_NAME..."
414 # The string passed to eval must handles spaces in paths correctly.
415 COMMAND_LINE
="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=$APP_NAME wrapper.pidfile=\"$PIDFILE\" wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $LOCKPROP"
418 echo "$APP_LONG_NAME is already running."
422 if [ "X$pid" != "X" ]
424 echo "Started $APP_LONG_NAME."
426 echo "Failed to start $APP_LONG_NAME."
431 echo "Stopping $APP_LONG_NAME..."
435 echo "$APP_LONG_NAME was not running."
437 if [ "X$IGNORE_SIGNALS" = "X" ]
439 # Running so try to stop it.
443 # An explanation for the failure should have been given
444 echo "Unable to stop $APP_LONG_NAME."
449 if [ -f "$ANCHORFILE" ]
451 # An explanation for the failure should have been given
452 echo "Unable to stop $APP_LONG_NAME."
457 # We can not predict how long it will take for the wrapper to
458 # actually stop as it depends on settings in wrapper.conf.
459 # Loop until it does.
463 while [ "X$pid" != "X" ]
465 # Show a waiting message every 5 seconds.
466 if [ "$CNT" -lt "5" ]
470 echo "Waiting for $APP_LONG_NAME to exit..."
473 TOTCNT
=`expr $TOTCNT + 1`
482 if [ "X$pid" != "X" ]
484 echo "Failed to stop $APP_LONG_NAME."
487 echo "Stopped $APP_LONG_NAME."
496 echo "$APP_LONG_NAME is not running."
499 echo "$APP_LONG_NAME is running ($pid)."
505 echo "Dumping $APP_LONG_NAME..."
509 echo "$APP_LONG_NAME was not running."
516 echo "Failed to dump $APP_LONG_NAME."
519 echo "Dumped $APP_LONG_NAME."
527 checkUser touchlock
$1
532 checkUser touchlock
$1
542 checkUser touchlock
$1
558 echo "Usage: $0 { console | start | stop | restart | status | dump }"