Versionbump hudson
[archlinuxdevstack.git] / nexus / nexus
blob6cfcc35838273636e43783f10f496d674ca7594a
1 #! /bin/sh
3 # The following changes are made for the ArchLinux nexus package
4 # Contributor: Markus M. May <mmay AT javafreedom DOT org>
6 export NEXUS_HOME=${NEXUS_HOME:=/opt/nexus}
7 export NEXUS_RUN_DIR=${NEXUS_RUN_DIR:=/var/run/nexus}
8 export PLEXUS_NEXUS_WORK=${PLEXUS_NEXUS_WORK:=/var/lib/nexus}
11 # Copyright (c) 1999, 2006 Tanuki Software Inc.
13 # Java Service Wrapper sh script. Suitable for starting and stopping
14 # wrapped Java applications on UNIX platforms.
16 #-----------------------------------------------------------------------------
17 # These settings can be modified to fit the needs of your application
19 # Default values for the Application variables, below.
21 # NOTE: The build for specific applications may override this during the resource-copying
22 # phase, to fill in a concrete name and avoid the use of the defaults specified here.
23 DEF_APP_NAME="testwrapper"
24 DEF_APP_LONG_NAME="Test Wrapper Sample Application"
26 # Application
27 APP_NAME="nexus-webapp"
28 APP_LONG_NAME="Sonatype Nexus Repository Manager"
30 # Wrapper
31 WRAPPER_CMD="$NEXUS_HOME/bin/wrapper"
32 WRAPPER_CONF="$NEXUS_HOME/conf/wrapper.conf"
34 # Priority at which to run the wrapper. See "man nice" for valid priorities.
35 # nice is only used if a priority is specified.
36 PRIORITY=
38 # Location of the pid file.
39 PIDDIR="$NEXUS_RUN_DIR"
41 # If uncommented, causes the Wrapper to be shutdown using an anchor file.
42 # When launched with the 'start' command, it will also ignore all INT and
43 # TERM signals.
44 #IGNORE_SIGNALS=true
46 # If specified, the Wrapper will be run as the specified user.
47 # IMPORTANT - Make sure that the user has the required privileges to write
48 # the PID file and wrapper.log files. Failure to be able to write the log
49 # file will cause the Wrapper to exit without any way to write out an error
50 # message.
51 # NOTE - This will set the user which is used to run the Wrapper as well as
52 # the JVM and is not useful in situations where a privileged resource or
53 # port needs to be allocated prior to the user being changed.
54 RUN_AS_USER=nexus
56 # The following two lines are used by the chkconfig command. Change as is
57 # appropriate for your application. They should remain commented.
58 # chkconfig: 2345 20 80
59 # description: Test Wrapper Sample Application
61 # Do not modify anything beyond this point
62 #-----------------------------------------------------------------------------
64 # Get the fully qualified path to the script
65 case $0 in
66 /*)
67 SCRIPT="$0"
70 PWD=`pwd`
71 SCRIPT="$PWD/$0"
73 esac
75 # Resolve the true real path without any sym links.
76 CHANGED=true
77 while [ "X$CHANGED" != "X" ]
79 # Change spaces to ":" so the tokens can be parsed.
80 SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
81 # Get the real path to this script, resolving any symbolic links
82 TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
83 REALPATH=
84 for C in $TOKENS; do
85 # Change any ":" in the token back to a space.
86 C=`echo $C | sed -e 's;:; ;g'`
87 REALPATH="$REALPATH/$C"
88 # If REALPATH is a sym link, resolve it. Loop for nested links.
89 while [ -h "$REALPATH" ] ; do
90 LS="`ls -ld "$REALPATH"`"
91 LINK="`expr "$LS" : '.*-> \(.*\)$'`"
92 if expr "$LINK" : '/.*' > /dev/null; then
93 # LINK is absolute.
94 REALPATH="$LINK"
95 else
96 # LINK is relative.
97 REALPATH="`dirname "$REALPATH"`""/$LINK"
99 done
100 done
102 if [ "$REALPATH" = "$SCRIPT" ]
103 then
104 CHANGED=""
105 else
106 SCRIPT="$REALPATH"
108 done
110 # Change the current directory to the location of the script
111 cd "`dirname "$REALPATH"`"
112 REALDIR=`pwd`
114 # If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
115 # the working directory is later changed.
116 FIRST_CHAR=`echo $PIDDIR | cut -c1,1`
117 if [ "$FIRST_CHAR" != "/" ]
118 then
119 PIDDIR=$REALDIR/$PIDDIR
121 # Same test for WRAPPER_CMD
122 FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1`
123 if [ "$FIRST_CHAR" != "/" ]
124 then
125 WRAPPER_CMD=$REALDIR/$WRAPPER_CMD
127 # Same test for WRAPPER_CONF
128 FIRST_CHAR=`echo $WRAPPER_CONF | cut -c1,1`
129 if [ "$FIRST_CHAR" != "/" ]
130 then
131 WRAPPER_CONF=$REALDIR/$WRAPPER_CONF
134 # Process ID
135 ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
136 PIDFILE="$PIDDIR/$APP_NAME.pid"
137 LOCKDIR="/var/lock/subsys"
138 LOCKFILE="$LOCKDIR/$APP_NAME"
139 pid=""
141 # Resolve the location of the 'ps' command
142 PSEXE="/usr/bin/ps"
143 if [ ! -x "$PSEXE" ]
144 then
145 PSEXE="/bin/ps"
146 if [ ! -x "$PSEXE" ]
147 then
148 echo "Unable to locate 'ps'."
149 echo "Please report this message along with the location of the command on your system."
150 exit 1
154 # Resolve the os
155 DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
156 case "$DIST_OS" in
157 'sunos')
158 DIST_OS="solaris"
160 'hp-ux' | 'hp-ux64')
161 DIST_OS="hpux"
163 'darwin')
164 DIST_OS="macosx"
166 'unix_sv')
167 DIST_OS="unixware"
169 esac
171 # Resolve the architecture
172 DIST_ARCH=`uname -p | tr [:upper:] [:lower:] | tr -d [:blank:]`
173 if [ "$DIST_ARCH" = "unknown" ]
174 then
175 DIST_ARCH=`uname -m | tr [:upper:] [:lower:] | tr -d [:blank:]`
177 case "$DIST_ARCH" in
178 'amd64' | 'athlon' | 'ia32' | 'ia64' | 'i386' | 'i486' | 'i586' | 'i686' | 'x86_64')
179 DIST_ARCH="x86"
181 'ip27')
182 DIST_ARCH="mips"
184 'power' | 'powerpc' | 'power_pc' | 'ppc64')
185 DIST_ARCH="ppc"
187 'pa_risc' | 'pa-risc')
188 DIST_ARCH="parisc"
190 'sun4u' | 'sparcv9')
191 DIST_ARCH="sparc"
193 '9000/800')
194 DIST_ARCH="parisc"
196 esac
198 outputFile() {
199 if [ -f "$1" ]
200 then
201 echo " $1 (Found but not executable.)";
202 else
203 echo " $1"
207 # Decide on the wrapper binary to use.
208 # If a 32-bit wrapper binary exists then it will work on 32 or 64 bit
209 # platforms, if the 64-bit binary exists then the distribution most
210 # likely wants to use long names. Otherwise, look for the default.
211 # For macosx, we also want to look for universal binaries.
212 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
213 if [ -x "$WRAPPER_TEST_CMD" ]
214 then
215 WRAPPER_CMD="$WRAPPER_TEST_CMD"
216 else
217 if [ "$DIST_OS" = "macosx" ]
218 then
219 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-32"
220 if [ -x "$WRAPPER_TEST_CMD" ]
221 then
222 WRAPPER_CMD="$WRAPPER_TEST_CMD"
223 else
224 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
225 if [ -x "$WRAPPER_TEST_CMD" ]
226 then
227 WRAPPER_CMD="$WRAPPER_TEST_CMD"
228 else
229 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-64"
230 if [ -x "$WRAPPER_TEST_CMD" ]
231 then
232 WRAPPER_CMD="$WRAPPER_TEST_CMD"
233 else
234 if [ ! -x "$WRAPPER_CMD" ]
235 then
236 echo "Unable to locate any of the following binaries:"
237 outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
238 outputFile "$WRAPPER_CMD-$DIST_OS-universal-32"
239 outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
240 outputFile "$WRAPPER_CMD-$DIST_OS-universal-64"
241 outputFile "$WRAPPER_CMD"
242 exit 1
247 else
248 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
249 if [ -x "$WRAPPER_TEST_CMD" ]
250 then
251 WRAPPER_CMD="$WRAPPER_TEST_CMD"
252 else
253 if [ ! -x "$WRAPPER_CMD" ]
254 then
255 echo "Unable to locate any of the following binaries:"
256 outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
257 outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
258 outputFile "$WRAPPER_CMD"
259 exit 1
265 # Build the nice clause
266 if [ "X$PRIORITY" = "X" ]
267 then
268 CMDNICE=""
269 else
270 CMDNICE="nice -$PRIORITY"
273 # Build the anchor file clause.
274 if [ "X$IGNORE_SIGNALS" = "X" ]
275 then
276 ANCHORPROP=
277 IGNOREPROP=
278 else
279 ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
280 IGNOREPROP=wrapper.ignore_signals=TRUE
283 # Build the lock file clause. Only create a lock file if the lock directory exists on this platform.
284 LOCKPROP=
285 if [ -d $LOCKDIR ]
286 then
287 if [ -w $LOCKDIR ]
288 then
289 LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
293 checkUser() {
294 # $1 touchLock flag
295 # $2 command
297 # Check the configured user. If necessary rerun this script as the desired user.
298 if [ "X$RUN_AS_USER" != "X" ]
299 then
300 # Resolve the location of the 'id' command
301 IDEXE="/usr/xpg4/bin/id"
302 if [ ! -x "$IDEXE" ]
303 then
304 IDEXE="/usr/bin/id"
305 if [ ! -x "$IDEXE" ]
306 then
307 echo "Unable to locate 'id'."
308 echo "Please report this message along with the location of the command on your system."
309 exit 1
313 if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
314 then
315 # Already running as the configured user. Avoid password prompts by not calling su.
316 RUN_AS_USER=""
319 if [ "X$RUN_AS_USER" != "X" ]
320 then
321 # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
322 # able to create the lock file. The Wrapper will be able to update this file once it
323 # is created but will not be able to delete it on shutdown. If $2 is defined then
324 # the lock file should be created for the current command
325 if [ "X$LOCKPROP" != "X" ]
326 then
327 if [ "X$1" != "X" ]
328 then
329 # Resolve the primary group
330 RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
331 if [ "X$RUN_AS_GROUP" = "X" ]
332 then
333 RUN_AS_GROUP=$RUN_AS_USER
335 touch $LOCKFILE
336 chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
340 # Still want to change users, recurse. This means that the user will only be
341 # prompted for a password once. Variables shifted by 1
342 su -m $RUN_AS_USER -c "\"$REALPATH\" $2"
344 # Now that we are the original user again, we may need to clean up the lock file.
345 if [ "X$LOCKPROP" != "X" ]
346 then
347 getpid
348 if [ "X$pid" = "X" ]
349 then
350 # Wrapper is not running so make sure the lock file is deleted.
351 if [ -f "$LOCKFILE" ]
352 then
353 rm "$LOCKFILE"
358 exit 0
362 getpid() {
363 if [ -f "$PIDFILE" ]
364 then
365 if [ -r "$PIDFILE" ]
366 then
367 pid=`cat "$PIDFILE"`
368 if [ "X$pid" != "X" ]
369 then
370 # It is possible that 'a' process with the pid exists but that it is not the
371 # correct process. This can happen in a number of cases, but the most
372 # common is during system startup after an unclean shutdown.
373 # The ps statement below looks for the specific wrapper command running as
374 # the pid. If it is not found then the pid file is considered to be stale.
375 pidtest=`$PSEXE -p $pid -o args | grep "$WRAPPER_CMD" | tail -1`
376 if [ "X$pidtest" = "X" ]
377 then
378 # This is a stale pid file.
379 rm -f "$PIDFILE"
380 echo "Removed stale pid file: $PIDFILE"
381 pid=""
384 else
385 echo "Cannot read $PIDFILE."
386 exit 1
391 testpid() {
392 pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
393 if [ "X$pid" = "X" ]
394 then
395 # Process is gone so remove the pid file.
396 rm -f "$PIDFILE"
397 pid=""
401 console() {
402 echo "Running $APP_LONG_NAME..."
403 getpid
404 if [ "X$pid" = "X" ]
405 then
406 # The string passed to eval must handles spaces in paths correctly.
407 COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=$APP_NAME wrapper.pidfile=\"$PIDFILE\" $ANCHORPROP $LOCKPROP"
408 eval $COMMAND_LINE
409 else
410 echo "$APP_LONG_NAME is already running."
411 exit 1
415 start() {
416 echo "Starting $APP_LONG_NAME..."
417 getpid
418 if [ "X$pid" = "X" ]
419 then
420 # The string passed to eval must handles spaces in paths correctly.
421 COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=$APP_NAME wrapper.pidfile=\"$PIDFILE\" wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $LOCKPROP"
422 eval $COMMAND_LINE
423 else
424 echo "$APP_LONG_NAME is already running."
425 exit 1
427 getpid
428 if [ "X$pid" != "X" ]
429 then
430 echo "Started $APP_LONG_NAME."
431 else
432 echo "Failed to start $APP_LONG_NAME."
436 stopit() {
437 echo "Stopping $APP_LONG_NAME..."
438 getpid
439 if [ "X$pid" = "X" ]
440 then
441 echo "$APP_LONG_NAME was not running."
442 else
443 if [ "X$IGNORE_SIGNALS" = "X" ]
444 then
445 # Running so try to stop it.
446 kill $pid
447 if [ $? -ne 0 ]
448 then
449 # An explanation for the failure should have been given
450 echo "Unable to stop $APP_LONG_NAME."
451 exit 1
453 else
454 rm -f "$ANCHORFILE"
455 if [ -f "$ANCHORFILE" ]
456 then
457 # An explanation for the failure should have been given
458 echo "Unable to stop $APP_LONG_NAME."
459 exit 1
463 # We can not predict how long it will take for the wrapper to
464 # actually stop as it depends on settings in wrapper.conf.
465 # Loop until it does.
466 savepid=$pid
467 CNT=0
468 TOTCNT=0
469 while [ "X$pid" != "X" ]
471 # Show a waiting message every 5 seconds.
472 if [ "$CNT" -lt "5" ]
473 then
474 CNT=`expr $CNT + 1`
475 else
476 echo "Waiting for $APP_LONG_NAME to exit..."
477 CNT=0
479 TOTCNT=`expr $TOTCNT + 1`
481 sleep 1
483 testpid
484 done
486 pid=$savepid
487 testpid
488 if [ "X$pid" != "X" ]
489 then
490 echo "Failed to stop $APP_LONG_NAME."
491 exit 1
492 else
493 echo "Stopped $APP_LONG_NAME."
498 status() {
499 getpid
500 if [ "X$pid" = "X" ]
501 then
502 echo "$APP_LONG_NAME is not running."
503 exit 1
504 else
505 echo "$APP_LONG_NAME is running ($pid)."
506 exit 0
510 dump() {
511 echo "Dumping $APP_LONG_NAME..."
512 getpid
513 if [ "X$pid" = "X" ]
514 then
515 echo "$APP_LONG_NAME was not running."
517 else
518 kill -3 $pid
520 if [ $? -ne 0 ]
521 then
522 echo "Failed to dump $APP_LONG_NAME."
523 exit 1
524 else
525 echo "Dumped $APP_LONG_NAME."
530 case "$1" in
532 'console')
533 checkUser touchlock $1
534 console
537 'start')
538 checkUser touchlock $1
539 start
542 'stop')
543 checkUser "" $1
544 stopit
547 'restart')
548 checkUser touchlock $1
549 stopit
550 start
553 'status')
554 checkUser "" $1
555 status
558 'dump')
559 checkUser "" $1
560 dump
564 echo "Usage: $0 { console | start | stop | restart | status | dump }"
565 exit 1
567 esac
569 exit 0