Clean up Hudson
[archlinuxdevstack.git] / sonar / sonar.sh
blobd31b9438a08302a8e9bd5d5d675622a4baaa31f8
1 #! /bin/sh
4 # Copyright (c) 1999, 2008 Tanuki Software, Inc.
5 # http://www.tanukisoftware.com
6 # All rights reserved.
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
27 # under archlinux.
29 # Application
30 APP_NAME="sonar"
31 APP_LONG_NAME="sonar"
33 # Wrapper
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.
39 PRIORITY=
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
46 # TERM signals.
47 #IGNORE_SIGNALS=true
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
54 # (in seconds).
56 WAIT_AFTER_STARTUP=0
58 # If set, the status, start_msg and stop_msg commands will print out detailed
59 # state information on the Wrapper and Java processes.
60 DETAIL_STATUS=true
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
66 # message.
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.
70 RUN_AS_USER=sonar
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.
79 ### BEGIN INIT INFO
80 # Provides: @app.name@
81 # Required-Start: $local_fs $network $syslog
82 # Should-Start:
83 # Required-Stop:
84 # Default-Start: 2 3 4 5
85 # Default-Stop: 0 1 6
86 # Short-Description: @app.long.name@
87 # Description: @app.description@
88 ### END INIT INFO
90 # Do not modify anything beyond this point
91 #-----------------------------------------------------------------------------
93 # Get the fully qualified path to the script
94 case $0 in
95 /*)
96 SCRIPT="$0"
99 PWD=`pwd`
100 SCRIPT="$PWD/$0"
102 esac
104 # Resolve the true real path without any sym links.
105 CHANGED=true
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'`
112 REALPATH=
113 for C in $TOKENS; do
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
122 # LINK is absolute.
123 REALPATH="$LINK"
124 else
125 # LINK is relative.
126 REALPATH="`dirname "$REALPATH"`""/$LINK"
128 done
129 done
131 if [ "$REALPATH" = "$SCRIPT" ]
132 then
133 CHANGED=""
134 else
135 SCRIPT="$REALPATH"
137 done
139 # Change the current directory to the location of the script
140 cd "`dirname "$REALPATH"`"
141 REALDIR=`pwd`
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" != "/" ]
147 then
148 PIDDIR=$REALDIR/$PIDDIR
150 # Same test for WRAPPER_CMD
151 FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1`
152 if [ "$FIRST_CHAR" != "/" ]
153 then
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" != "/" ]
159 then
160 WRAPPER_CONF=$REALDIR/$WRAPPER_CONF
163 # Process ID
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"
170 pid=""
172 # Resolve the location of the 'ps' command
173 PSEXE="/usr/bin/ps"
174 if [ ! -x "$PSEXE" ]
175 then
176 PSEXE="/bin/ps"
177 if [ ! -x "$PSEXE" ]
178 then
179 echo "Unable to locate 'ps'."
180 echo "Please report this message along with the location of the command on your system."
181 exit 1
185 # Resolve the os
186 DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
187 case "$DIST_OS" in
188 'sunos')
189 DIST_OS="solaris"
191 'hp-ux' | 'hp-ux64')
192 # HP-UX needs the XPG4 version of ps (for -o args)
193 DIST_OS="hpux"
194 UNIX95=""
195 export UNIX95
197 'darwin')
198 DIST_OS="macosx"
200 'unix_sv')
201 DIST_OS="unixware"
203 esac
205 # Resolve the architecture
206 if [ "$DIST_OS" = "macosx" ]
207 then
208 DIST_ARCH="universal"
209 else
210 DIST_ARCH=
211 DIST_ARCH=`uname -p 2>/dev/null | tr [:upper:] [:lower:] | tr -d [:blank:]`
212 if [ "X$DIST_ARCH" = "X" ]
213 then
214 DIST_ARCH="unknown"
216 if [ "$DIST_ARCH" = "unknown" ]
217 then
218 DIST_ARCH=`uname -m 2>/dev/null | tr [:upper:] [:lower:] | tr -d [:blank:]`
220 case "$DIST_ARCH" in
221 'amd64' | 'athlon' | 'i386' | 'i486' | 'i586' | 'i686' | 'x86_64')
222 DIST_ARCH="x86"
224 'ia32' | 'ia64' | 'ia64n' | 'ia64w')
225 DIST_ARCH="ia"
227 'ip27')
228 DIST_ARCH="mips"
230 'power' | 'powerpc' | 'power_pc' | 'ppc64')
231 DIST_ARCH="ppc"
233 'pa_risc' | 'pa-risc')
234 DIST_ARCH="parisc"
236 'sun4u' | 'sparcv9')
237 DIST_ARCH="sparc"
239 '9000/800')
240 DIST_ARCH="parisc"
242 esac
245 # OSX always places Java in the same location so we can reliably set JAVA_HOME
246 if [ "$DIST_OS" = "macosx" ]
247 then
248 if [ -z "$JAVA_HOME" ]; then
249 JAVA_HOME="/Library/Java/Home"; export JAVA_HOME
253 outputFile() {
254 if [ -f "$1" ]
255 then
256 echo " $1 (Found but not executable.)";
257 else
258 echo " $1"
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" ]
268 then
269 WRAPPER_CMD="$WRAPPER_TEST_CMD"
270 else
271 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
272 if [ -x "$WRAPPER_TEST_CMD" ]
273 then
274 WRAPPER_CMD="$WRAPPER_TEST_CMD"
275 else
276 if [ ! -x "$WRAPPER_CMD" ]
277 then
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"
282 exit 1
287 # Build the nice clause
288 if [ "X$PRIORITY" = "X" ]
289 then
290 CMDNICE=""
291 else
292 CMDNICE="nice -$PRIORITY"
295 # Build the anchor file clause.
296 if [ "X$IGNORE_SIGNALS" = "X" ]
297 then
298 ANCHORPROP=
299 IGNOREPROP=
300 else
301 ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
302 IGNOREPROP=wrapper.ignore_signals=TRUE
305 # Build the status file clause.
306 if [ "X$DETAIL_STATUS" = "X" ]
307 then
308 STATUSPROP=
309 else
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.
314 LOCKPROP=
315 if [ -d $LOCKDIR ]
316 then
317 if [ -w $LOCKDIR ]
318 then
319 LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
323 checkUser() {
324 # $1 touchLock flag
325 # $2 command
327 # Check the configured user. If necessary rerun this script as the desired user.
328 if [ "X$RUN_AS_USER" != "X" ]
329 then
330 # Resolve the location of the 'id' command
331 IDEXE="/usr/xpg4/bin/id"
332 if [ ! -x "$IDEXE" ]
333 then
334 IDEXE="/usr/bin/id"
335 if [ ! -x "$IDEXE" ]
336 then
337 echo "Unable to locate 'id'."
338 echo "Please report this message along with the location of the command on your system."
339 exit 1
343 if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
344 then
345 # Already running as the configured user. Avoid password prompts by not calling su.
346 RUN_AS_USER=""
349 if [ "X$RUN_AS_USER" != "X" ]
350 then
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" ]
356 then
357 if [ "X$1" != "X" ]
358 then
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" ]
362 then
363 RUN_AS_GROUP=$RUN_AS_USER
365 touch $LOCKFILE
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"
376 then
377 /sbin/runuser - $RUN_AS_USER -c "\"$REALPATH\" $2"
378 else
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" ]
384 then
385 getpid
386 if [ "X$pid" = "X" ]
387 then
388 # Wrapper is not running so make sure the lock file is deleted.
389 if [ -f "$LOCKFILE" ]
390 then
391 rm "$LOCKFILE"
396 exit 0
400 getpid() {
401 pid=""
402 if [ -f "$PIDFILE" ]
403 then
404 if [ -r "$PIDFILE" ]
405 then
406 pid=`cat "$PIDFILE"`
407 if [ "X$pid" != "X" ]
408 then
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.
414 case "$DIST_OS" in
415 'macosx')
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`
421 esac
423 if [ "X$pidtest" = "X" ]
424 then
425 # This is a stale pid file.
426 rm -f "$PIDFILE"
427 echo "Removed stale pid file: $PIDFILE"
428 pid=""
431 else
432 echo "Cannot read $PIDFILE."
433 exit 1
438 getstatus() {
439 STATUS=
440 if [ -f "$STATUSFILE" ]
441 then
442 if [ -r "$STATUSFILE" ]
443 then
444 STATUS=`cat "$STATUSFILE"`
447 if [ "X$STATUS" = "X" ]
448 then
449 STATUS="Unknown"
452 JAVASTATUS=
453 if [ -f "$JAVASTATUSFILE" ]
454 then
455 if [ -r "$JAVASTATUSFILE" ]
456 then
457 JAVASTATUS=`cat "$JAVASTATUSFILE"`
460 if [ "X$JAVASTATUS" = "X" ]
461 then
462 JAVASTATUS="Unknown"
466 testpid() {
467 pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
468 if [ "X$pid" = "X" ]
469 then
470 # Process is gone so remove the pid file.
471 rm -f "$PIDFILE"
472 pid=""
476 console() {
477 echo "Running $APP_LONG_NAME..."
478 getpid
479 if [ "X$pid" = "X" ]
480 then
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"
483 eval $COMMAND_LINE
484 else
485 echo "$APP_LONG_NAME is already running."
486 exit 1
490 start() {
491 echo -n "Starting $APP_LONG_NAME..."
492 getpid
493 if [ "X$pid" = "X" ]
494 then
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"
497 eval $COMMAND_LINE
498 else
499 echo "$APP_LONG_NAME is already running."
500 exit 1
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 ]
509 sleep 1
510 echo -n "."
511 i=`expr $i + 1`
512 done
513 if [ $WAIT_AFTER_STARTUP -gt 0 ]
514 then
515 getpid
516 if [ "X$pid" = "X" ]
517 then
518 echo " WARNING: $APP_LONG_NAME may have failed to start."
519 exit 1
520 else
521 echo " running ($pid)."
523 else
524 echo ""
528 stopit() {
529 echo "Stopping $APP_LONG_NAME..."
530 getpid
531 if [ "X$pid" = "X" ]
532 then
533 echo "$APP_LONG_NAME was not running."
534 else
535 if [ "X$IGNORE_SIGNALS" = "X" ]
536 then
537 # Running so try to stop it.
538 kill $pid
539 if [ $? -ne 0 ]
540 then
541 # An explanation for the failure should have been given
542 echo "Unable to stop $APP_LONG_NAME."
543 exit 1
545 else
546 rm -f "$ANCHORFILE"
547 if [ -f "$ANCHORFILE" ]
548 then
549 # An explanation for the failure should have been given
550 echo "Unable to stop $APP_LONG_NAME."
551 exit 1
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.
558 savepid=$pid
559 CNT=0
560 TOTCNT=0
561 while [ "X$pid" != "X" ]
563 # Show a waiting message every 5 seconds.
564 if [ "$CNT" -lt "5" ]
565 then
566 CNT=`expr $CNT + 1`
567 else
568 echo "Waiting for $APP_LONG_NAME to exit..."
569 CNT=0
571 TOTCNT=`expr $TOTCNT + 1`
573 sleep 1
575 testpid
576 done
578 pid=$savepid
579 testpid
580 if [ "X$pid" != "X" ]
581 then
582 echo "Failed to stop $APP_LONG_NAME."
583 exit 1
584 else
585 echo "Stopped $APP_LONG_NAME."
590 status() {
591 getpid
592 if [ "X$pid" = "X" ]
593 then
594 echo "$APP_LONG_NAME is not running."
595 exit 1
596 else
597 if [ "X$DETAIL_STATUS" = "X" ]
598 then
599 echo "$APP_LONG_NAME is running (PID:$pid)."
600 else
601 getstatus
602 echo "$APP_LONG_NAME is running (PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS)"
604 exit 0
608 dump() {
609 echo "Dumping $APP_LONG_NAME..."
610 getpid
611 if [ "X$pid" = "X" ]
612 then
613 echo "$APP_LONG_NAME was not running."
614 else
615 kill -3 $pid
617 if [ $? -ne 0 ]
618 then
619 echo "Failed to dump $APP_LONG_NAME."
620 exit 1
621 else
622 echo "Dumped $APP_LONG_NAME."
627 # Used by HP-UX init scripts.
628 startmsg() {
629 getpid
630 if [ "X$pid" = "X" ]
631 then
632 echo "Starting $APP_LONG_NAME... (Wrapper:Stopped)"
633 else
634 if [ "X$DETAIL_STATUS" = "X" ]
635 then
636 echo "Starting $APP_LONG_NAME... (Wrapper:Running)"
637 else
638 getstatus
639 echo "Starting $APP_LONG_NAME... (Wrapper:$STATUS, Java:$JAVASTATUS)"
644 # Used by HP-UX init scripts.
645 stopmsg() {
646 getpid
647 if [ "X$pid" = "X" ]
648 then
649 echo "Stopping $APP_LONG_NAME... (Wrapper:Stopped)"
650 else
651 if [ "X$DETAIL_STATUS" = "X" ]
652 then
653 echo "Stopping $APP_LONG_NAME... (Wrapper:Running)"
654 else
655 getstatus
656 echo "Stopping $APP_LONG_NAME... (Wrapper:$STATUS, Java:$JAVASTATUS)"
661 case "$1" in
663 'console')
664 checkUser touchlock $1
665 console
668 'start')
669 checkUser touchlock $1
670 start
673 'stop')
674 checkUser "" $1
675 stopit
678 'restart')
679 checkUser touchlock $1
680 stopit
681 start
684 'status')
685 checkUser "" $1
686 status
689 'dump')
690 checkUser "" $1
691 dump
694 'start_msg')
695 checkUser "" $1
696 startmsg
699 'stop_msg')
700 checkUser "" $1
701 stopmsg
705 echo "Usage: $0 { console | start | stop | restart | status | dump }"
706 exit 1
708 esac
710 exit 0