Major changes, preparation, so that Nexus can run unix-ified as well.
[archlinuxdevstack.git] / nexus / nexus.sh
blob34569e25f174b0f10c73ac02fa7942b8b3c0d402
1 #! /bin/sh
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"
20 # Application
21 APP_NAME="nexus-webapp"
22 APP_LONG_NAME="Sonatype Nexus Repository Manager"
24 # Wrapper
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.
30 PRIORITY=
32 # Location of the pid file.
33 PIDDIR="/var/run"
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
37 # TERM signals.
38 #IGNORE_SIGNALS=true
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
44 # message.
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.
48 #RUN_AS_USER=
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
59 case $0 in
60 /*)
61 SCRIPT="$0"
64 PWD=`pwd`
65 SCRIPT="$PWD/$0"
67 esac
69 # Resolve the true real path without any sym links.
70 CHANGED=true
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'`
77 REALPATH=
78 for C in $TOKENS; do
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
87 # LINK is absolute.
88 REALPATH="$LINK"
89 else
90 # LINK is relative.
91 REALPATH="`dirname "$REALPATH"`""/$LINK"
93 done
94 done
96 if [ "$REALPATH" = "$SCRIPT" ]
97 then
98 CHANGED=""
99 else
100 SCRIPT="$REALPATH"
102 done
104 # Change the current directory to the location of the script
105 cd "`dirname "$REALPATH"`"
106 REALDIR=`pwd`
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" != "/" ]
112 then
113 PIDDIR=$REALDIR/$PIDDIR
115 # Same test for WRAPPER_CMD
116 FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1`
117 if [ "$FIRST_CHAR" != "/" ]
118 then
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" != "/" ]
124 then
125 WRAPPER_CONF=$REALDIR/$WRAPPER_CONF
128 # Process ID
129 ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
130 PIDFILE="$PIDDIR/$APP_NAME.pid"
131 LOCKDIR="/var/lock/subsys"
132 LOCKFILE="$LOCKDIR/$APP_NAME"
133 pid=""
135 # Resolve the location of the 'ps' command
136 PSEXE="/usr/bin/ps"
137 if [ ! -x "$PSEXE" ]
138 then
139 PSEXE="/bin/ps"
140 if [ ! -x "$PSEXE" ]
141 then
142 echo "Unable to locate 'ps'."
143 echo "Please report this message along with the location of the command on your system."
144 exit 1
148 # Resolve the os
149 DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
150 case "$DIST_OS" in
151 'sunos')
152 DIST_OS="solaris"
154 'hp-ux' | 'hp-ux64')
155 DIST_OS="hpux"
157 'darwin')
158 DIST_OS="macosx"
160 'unix_sv')
161 DIST_OS="unixware"
163 esac
165 # Resolve the architecture
166 DIST_ARCH=`uname -p | tr [:upper:] [:lower:] | tr -d [:blank:]`
167 if [ "$DIST_ARCH" = "unknown" ]
168 then
169 DIST_ARCH=`uname -m | tr [:upper:] [:lower:] | tr -d [:blank:]`
171 case "$DIST_ARCH" in
172 'amd64' | 'athlon' | 'ia32' | 'ia64' | 'i386' | 'i486' | 'i586' | 'i686' | 'x86_64')
173 DIST_ARCH="x86"
175 'ip27')
176 DIST_ARCH="mips"
178 'power' | 'powerpc' | 'power_pc' | 'ppc64')
179 DIST_ARCH="ppc"
181 'pa_risc' | 'pa-risc')
182 DIST_ARCH="parisc"
184 'sun4u' | 'sparcv9')
185 DIST_ARCH="sparc"
187 '9000/800')
188 DIST_ARCH="parisc"
190 esac
192 outputFile() {
193 if [ -f "$1" ]
194 then
195 echo " $1 (Found but not executable.)";
196 else
197 echo " $1"
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" ]
208 then
209 WRAPPER_CMD="$WRAPPER_TEST_CMD"
210 else
211 if [ "$DIST_OS" = "macosx" ]
212 then
213 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-32"
214 if [ -x "$WRAPPER_TEST_CMD" ]
215 then
216 WRAPPER_CMD="$WRAPPER_TEST_CMD"
217 else
218 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
219 if [ -x "$WRAPPER_TEST_CMD" ]
220 then
221 WRAPPER_CMD="$WRAPPER_TEST_CMD"
222 else
223 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-64"
224 if [ -x "$WRAPPER_TEST_CMD" ]
225 then
226 WRAPPER_CMD="$WRAPPER_TEST_CMD"
227 else
228 if [ ! -x "$WRAPPER_CMD" ]
229 then
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"
236 exit 1
241 else
242 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
243 if [ -x "$WRAPPER_TEST_CMD" ]
244 then
245 WRAPPER_CMD="$WRAPPER_TEST_CMD"
246 else
247 if [ ! -x "$WRAPPER_CMD" ]
248 then
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"
253 exit 1
259 # Build the nice clause
260 if [ "X$PRIORITY" = "X" ]
261 then
262 CMDNICE=""
263 else
264 CMDNICE="nice -$PRIORITY"
267 # Build the anchor file clause.
268 if [ "X$IGNORE_SIGNALS" = "X" ]
269 then
270 ANCHORPROP=
271 IGNOREPROP=
272 else
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.
278 LOCKPROP=
279 if [ -d $LOCKDIR ]
280 then
281 if [ -w $LOCKDIR ]
282 then
283 LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
287 checkUser() {
288 # $1 touchLock flag
289 # $2 command
291 # Check the configured user. If necessary rerun this script as the desired user.
292 if [ "X$RUN_AS_USER" != "X" ]
293 then
294 # Resolve the location of the 'id' command
295 IDEXE="/usr/xpg4/bin/id"
296 if [ ! -x "$IDEXE" ]
297 then
298 IDEXE="/usr/bin/id"
299 if [ ! -x "$IDEXE" ]
300 then
301 echo "Unable to locate 'id'."
302 echo "Please report this message along with the location of the command on your system."
303 exit 1
307 if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
308 then
309 # Already running as the configured user. Avoid password prompts by not calling su.
310 RUN_AS_USER=""
313 if [ "X$RUN_AS_USER" != "X" ]
314 then
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" ]
320 then
321 if [ "X$1" != "X" ]
322 then
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" ]
326 then
327 RUN_AS_GROUP=$RUN_AS_USER
329 touch $LOCKFILE
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" ]
340 then
341 getpid
342 if [ "X$pid" = "X" ]
343 then
344 # Wrapper is not running so make sure the lock file is deleted.
345 if [ -f "$LOCKFILE" ]
346 then
347 rm "$LOCKFILE"
352 exit 0
356 getpid() {
357 if [ -f "$PIDFILE" ]
358 then
359 if [ -r "$PIDFILE" ]
360 then
361 pid=`cat "$PIDFILE"`
362 if [ "X$pid" != "X" ]
363 then
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" ]
371 then
372 # This is a stale pid file.
373 rm -f "$PIDFILE"
374 echo "Removed stale pid file: $PIDFILE"
375 pid=""
378 else
379 echo "Cannot read $PIDFILE."
380 exit 1
385 testpid() {
386 pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
387 if [ "X$pid" = "X" ]
388 then
389 # Process is gone so remove the pid file.
390 rm -f "$PIDFILE"
391 pid=""
395 console() {
396 echo "Running $APP_LONG_NAME..."
397 getpid
398 if [ "X$pid" = "X" ]
399 then
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"
402 eval $COMMAND_LINE
403 else
404 echo "$APP_LONG_NAME is already running."
405 exit 1
409 start() {
410 echo "Starting $APP_LONG_NAME..."
411 getpid
412 if [ "X$pid" = "X" ]
413 then
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"
416 eval $COMMAND_LINE
417 else
418 echo "$APP_LONG_NAME is already running."
419 exit 1
421 getpid
422 if [ "X$pid" != "X" ]
423 then
424 echo "Started $APP_LONG_NAME."
425 else
426 echo "Failed to start $APP_LONG_NAME."
430 stopit() {
431 echo "Stopping $APP_LONG_NAME..."
432 getpid
433 if [ "X$pid" = "X" ]
434 then
435 echo "$APP_LONG_NAME was not running."
436 else
437 if [ "X$IGNORE_SIGNALS" = "X" ]
438 then
439 # Running so try to stop it.
440 kill $pid
441 if [ $? -ne 0 ]
442 then
443 # An explanation for the failure should have been given
444 echo "Unable to stop $APP_LONG_NAME."
445 exit 1
447 else
448 rm -f "$ANCHORFILE"
449 if [ -f "$ANCHORFILE" ]
450 then
451 # An explanation for the failure should have been given
452 echo "Unable to stop $APP_LONG_NAME."
453 exit 1
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.
460 savepid=$pid
461 CNT=0
462 TOTCNT=0
463 while [ "X$pid" != "X" ]
465 # Show a waiting message every 5 seconds.
466 if [ "$CNT" -lt "5" ]
467 then
468 CNT=`expr $CNT + 1`
469 else
470 echo "Waiting for $APP_LONG_NAME to exit..."
471 CNT=0
473 TOTCNT=`expr $TOTCNT + 1`
475 sleep 1
477 testpid
478 done
480 pid=$savepid
481 testpid
482 if [ "X$pid" != "X" ]
483 then
484 echo "Failed to stop $APP_LONG_NAME."
485 exit 1
486 else
487 echo "Stopped $APP_LONG_NAME."
492 status() {
493 getpid
494 if [ "X$pid" = "X" ]
495 then
496 echo "$APP_LONG_NAME is not running."
497 exit 1
498 else
499 echo "$APP_LONG_NAME is running ($pid)."
500 exit 0
504 dump() {
505 echo "Dumping $APP_LONG_NAME..."
506 getpid
507 if [ "X$pid" = "X" ]
508 then
509 echo "$APP_LONG_NAME was not running."
511 else
512 kill -3 $pid
514 if [ $? -ne 0 ]
515 then
516 echo "Failed to dump $APP_LONG_NAME."
517 exit 1
518 else
519 echo "Dumped $APP_LONG_NAME."
524 case "$1" in
526 'console')
527 checkUser touchlock $1
528 console
531 'start')
532 checkUser touchlock $1
533 start
536 'stop')
537 checkUser "" $1
538 stopit
541 'restart')
542 checkUser touchlock $1
543 stopit
544 start
547 'status')
548 checkUser "" $1
549 status
552 'dump')
553 checkUser "" $1
554 dump
558 echo "Usage: $0 { console | start | stop | restart | status | dump }"
559 exit 1
561 esac
563 exit 0