Minor changes to allow for storing all configuration in the Linux /etc directory...
[archlinuxdevstack.git] / sonar / sonar.sh
blob7f04b9464b14dc65c946af7a57691e57e7bdf25f
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.
18 #-----------------------------------------------------------------------------
19 # These settings can be modified to fit the needs of your application
20 # Optimized for use with version 3.3.0 of the Wrapper.
22 # These settings are highly modified for the use of the sonar application
23 # under archlinux.
25 # Linux czf00srv002 2.6.27-14-server #1 SMP Mon Aug 31 13:57:10 UTC 2009 i686 GNU/Linux
26 # Linux athene 2.6.31-ARCH #1 SMP PREEMPT Tue Oct 13 11:33:39 CEST 2009 x86_64 Intel(R) Core(TM)2 Duo CPU T9400 @ 2.53GHz GenuineIntel GNU/Linux
28 # Application
29 APP_NAME="sonar"
30 APP_LONG_NAME="sonar"
32 # Wrapper
33 WRAPPER_CMD="/opt/sonar/bin/wrapper"
34 WRAPPER_CONF="/etc/sonar/wrapper.conf"
36 # Priority at which to run the wrapper. See "man nice" for valid priorities.
37 # nice is only used if a priority is specified.
38 PRIORITY=
40 # Location of the pid file.
41 PIDDIR="/var/run"
43 # If uncommented, causes the Wrapper to be shutdown using an anchor file.
44 # When launched with the 'start' command, it will also ignore all INT and
45 # TERM signals.
46 #IGNORE_SIGNALS=true
48 # Wrapper will start the JVM asynchronously. Your application may have some
49 # initialization tasks and it may be desirable to wait a few seconds
50 # before returning. For example, to delay the invocation of following
51 # startup scripts. Setting WAIT_AFTER_STARTUP to a positive number will
52 # cause the start command to delay for the indicated period of time
53 # (in seconds).
55 WAIT_AFTER_STARTUP=0
57 # If set, the status, start_msg and stop_msg commands will print out detailed
58 # state information on the Wrapper and Java processes.
59 DETAIL_STATUS=true
61 # If specified, the Wrapper will be run as the specified user.
62 # IMPORTANT - Make sure that the user has the required privileges to write
63 # the PID file and wrapper.log files. Failure to be able to write the log
64 # file will cause the Wrapper to exit without any way to write out an error
65 # message.
66 # NOTE - This will set the user which is used to run the Wrapper as well as
67 # the JVM and is not useful in situations where a privileged resource or
68 # port needs to be allocated prior to the user being changed.
69 #RUN_AS_USER=
71 # The following two lines are used by the chkconfig command. Change as is
72 # appropriate for your application. They should remain commented.
73 # chkconfig: 2345 20 80
74 # description: "Sonar Server"
76 # Initialization block for the install_initd and remove_initd scripts used by
77 # SUSE linux distributions.
78 ### BEGIN INIT INFO
79 # Provides: @app.name@
80 # Required-Start: $local_fs $network $syslog
81 # Should-Start:
82 # Required-Stop:
83 # Default-Start: 2 3 4 5
84 # Default-Stop: 0 1 6
85 # Short-Description: @app.long.name@
86 # Description: @app.description@
87 ### END INIT INFO
89 # Do not modify anything beyond this point
90 #-----------------------------------------------------------------------------
92 # Get the fully qualified path to the script
93 case $0 in
94 /*)
95 SCRIPT="$0"
98 PWD=`pwd`
99 SCRIPT="$PWD/$0"
101 esac
103 # Resolve the true real path without any sym links.
104 CHANGED=true
105 while [ "X$CHANGED" != "X" ]
107 # Change spaces to ":" so the tokens can be parsed.
108 SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
109 # Get the real path to this script, resolving any symbolic links
110 TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
111 REALPATH=
112 for C in $TOKENS; do
113 # Change any ":" in the token back to a space.
114 C=`echo $C | sed -e 's;:; ;g'`
115 REALPATH="$REALPATH/$C"
116 # If REALPATH is a sym link, resolve it. Loop for nested links.
117 while [ -h "$REALPATH" ] ; do
118 LS="`ls -ld "$REALPATH"`"
119 LINK="`expr "$LS" : '.*-> \(.*\)$'`"
120 if expr "$LINK" : '/.*' > /dev/null; then
121 # LINK is absolute.
122 REALPATH="$LINK"
123 else
124 # LINK is relative.
125 REALPATH="`dirname "$REALPATH"`""/$LINK"
127 done
128 done
130 if [ "$REALPATH" = "$SCRIPT" ]
131 then
132 CHANGED=""
133 else
134 SCRIPT="$REALPATH"
136 done
138 # Change the current directory to the location of the script
139 cd "`dirname "$REALPATH"`"
140 REALDIR=`pwd`
142 # If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
143 # the working directory is later changed.
144 FIRST_CHAR=`echo $PIDDIR | cut -c1,1`
145 if [ "$FIRST_CHAR" != "/" ]
146 then
147 PIDDIR=$REALDIR/$PIDDIR
149 # Same test for WRAPPER_CMD
150 FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1`
151 if [ "$FIRST_CHAR" != "/" ]
152 then
153 WRAPPER_CMD=$REALDIR/$WRAPPER_CMD
155 # Same test for WRAPPER_CONF
156 FIRST_CHAR=`echo $WRAPPER_CONF | cut -c1,1`
157 if [ "$FIRST_CHAR" != "/" ]
158 then
159 WRAPPER_CONF=$REALDIR/$WRAPPER_CONF
162 # Process ID
163 ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
164 STATUSFILE="$PIDDIR/$APP_NAME.status"
165 JAVASTATUSFILE="$PIDDIR/$APP_NAME.java.status"
166 PIDFILE="$PIDDIR/$APP_NAME.pid"
167 LOCKDIR="/var/lock/subsys"
168 LOCKFILE="$LOCKDIR/$APP_NAME"
169 pid=""
171 # Resolve the location of the 'ps' command
172 PSEXE="/usr/bin/ps"
173 if [ ! -x "$PSEXE" ]
174 then
175 PSEXE="/bin/ps"
176 if [ ! -x "$PSEXE" ]
177 then
178 echo "Unable to locate 'ps'."
179 echo "Please report this message along with the location of the command on your system."
180 exit 1
184 # Resolve the os
185 DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
186 case "$DIST_OS" in
187 'sunos')
188 DIST_OS="solaris"
190 'hp-ux' | 'hp-ux64')
191 # HP-UX needs the XPG4 version of ps (for -o args)
192 DIST_OS="hpux"
193 UNIX95=""
194 export UNIX95
196 'darwin')
197 DIST_OS="macosx"
199 'unix_sv')
200 DIST_OS="unixware"
202 esac
204 # Resolve the architecture
205 if [ "$DIST_OS" = "macosx" ]
206 then
207 DIST_ARCH="universal"
208 else
209 DIST_ARCH=
210 DIST_ARCH=`uname -p 2>/dev/null | tr [:upper:] [:lower:] | tr -d [:blank:]`
211 if [ "X$DIST_ARCH" = "X" ]
212 then
213 DIST_ARCH="unknown"
215 if [ "$DIST_ARCH" = "unknown" ]
216 then
217 DIST_ARCH=`uname -m 2>/dev/null | tr [:upper:] [:lower:] | tr -d [:blank:]`
219 case "$DIST_ARCH" in
220 'amd64' | 'athlon' | 'i386' | 'i486' | 'i586' | 'i686' | 'x86_64')
221 DIST_ARCH="x86"
223 'ia32' | 'ia64' | 'ia64n' | 'ia64w')
224 DIST_ARCH="ia"
226 'ip27')
227 DIST_ARCH="mips"
229 'power' | 'powerpc' | 'power_pc' | 'ppc64')
230 DIST_ARCH="ppc"
232 'pa_risc' | 'pa-risc')
233 DIST_ARCH="parisc"
235 'sun4u' | 'sparcv9')
236 DIST_ARCH="sparc"
238 '9000/800')
239 DIST_ARCH="parisc"
241 esac
244 # OSX always places Java in the same location so we can reliably set JAVA_HOME
245 if [ "$DIST_OS" = "macosx" ]
246 then
247 if [ -z "$JAVA_HOME" ]; then
248 JAVA_HOME="/Library/Java/Home"; export JAVA_HOME
252 outputFile() {
253 if [ -f "$1" ]
254 then
255 echo " $1 (Found but not executable.)";
256 else
257 echo " $1"
261 # Decide on the wrapper binary to use.
262 # If a 32-bit wrapper binary exists then it will work on 32 or 64 bit
263 # platforms, if the 64-bit binary exists then the distribution most
264 # likely wants to use long names. Otherwise, look for the default.
265 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
266 if [ -x "$WRAPPER_TEST_CMD" ]
267 then
268 WRAPPER_CMD="$WRAPPER_TEST_CMD"
269 else
270 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
271 if [ -x "$WRAPPER_TEST_CMD" ]
272 then
273 WRAPPER_CMD="$WRAPPER_TEST_CMD"
274 else
275 if [ ! -x "$WRAPPER_CMD" ]
276 then
277 echo "Unable to locate any of the following binaries:"
278 outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
279 outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
280 outputFile "$WRAPPER_CMD"
281 exit 1
286 # Build the nice clause
287 if [ "X$PRIORITY" = "X" ]
288 then
289 CMDNICE=""
290 else
291 CMDNICE="nice -$PRIORITY"
294 # Build the anchor file clause.
295 if [ "X$IGNORE_SIGNALS" = "X" ]
296 then
297 ANCHORPROP=
298 IGNOREPROP=
299 else
300 ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
301 IGNOREPROP=wrapper.ignore_signals=TRUE
304 # Build the status file clause.
305 if [ "X$DETAIL_STATUS" = "X" ]
306 then
307 STATUSPROP=
308 else
309 STATUSPROP="wrapper.statusfile=\"$STATUSFILE\" wrapper.java.statusfile=\"$JAVASTATUSFILE\""
312 # Build the lock file clause. Only create a lock file if the lock directory exists on this platform.
313 LOCKPROP=
314 if [ -d $LOCKDIR ]
315 then
316 if [ -w $LOCKDIR ]
317 then
318 LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
322 checkUser() {
323 # $1 touchLock flag
324 # $2 command
326 # Check the configured user. If necessary rerun this script as the desired user.
327 if [ "X$RUN_AS_USER" != "X" ]
328 then
329 # Resolve the location of the 'id' command
330 IDEXE="/usr/xpg4/bin/id"
331 if [ ! -x "$IDEXE" ]
332 then
333 IDEXE="/usr/bin/id"
334 if [ ! -x "$IDEXE" ]
335 then
336 echo "Unable to locate 'id'."
337 echo "Please report this message along with the location of the command on your system."
338 exit 1
342 if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
343 then
344 # Already running as the configured user. Avoid password prompts by not calling su.
345 RUN_AS_USER=""
348 if [ "X$RUN_AS_USER" != "X" ]
349 then
350 # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
351 # able to create the lock file. The Wrapper will be able to update this file once it
352 # is created but will not be able to delete it on shutdown. If $2 is defined then
353 # the lock file should be created for the current command
354 if [ "X$LOCKPROP" != "X" ]
355 then
356 if [ "X$1" != "X" ]
357 then
358 # Resolve the primary group
359 RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
360 if [ "X$RUN_AS_GROUP" = "X" ]
361 then
362 RUN_AS_GROUP=$RUN_AS_USER
364 touch $LOCKFILE
365 chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
369 # Still want to change users, recurse. This means that the user will only be
370 # prompted for a password once. Variables shifted by 1
372 # Use "runuser" if this exists. runuser should be used on RedHat in preference to su.
374 if test -f "/sbin/runuser"
375 then
376 /sbin/runuser - $RUN_AS_USER -c "\"$REALPATH\" $2"
377 else
378 su - $RUN_AS_USER -c "\"$REALPATH\" $2"
381 # Now that we are the original user again, we may need to clean up the lock file.
382 if [ "X$LOCKPROP" != "X" ]
383 then
384 getpid
385 if [ "X$pid" = "X" ]
386 then
387 # Wrapper is not running so make sure the lock file is deleted.
388 if [ -f "$LOCKFILE" ]
389 then
390 rm "$LOCKFILE"
395 exit 0
399 getpid() {
400 pid=""
401 if [ -f "$PIDFILE" ]
402 then
403 if [ -r "$PIDFILE" ]
404 then
405 pid=`cat "$PIDFILE"`
406 if [ "X$pid" != "X" ]
407 then
408 # It is possible that 'a' process with the pid exists but that it is not the
409 # correct process. This can happen in a number of cases, but the most
410 # common is during system startup after an unclean shutdown.
411 # The ps statement below looks for the specific wrapper command running as
412 # the pid. If it is not found then the pid file is considered to be stale.
413 case "$DIST_OS" in
414 'macosx')
415 pidtest=`$PSEXE -ww -p $pid -o command | grep "$WRAPPER_CMD" | tail -1`
418 pidtest=`$PSEXE -p $pid -o args | grep "$WRAPPER_CMD" | tail -1`
420 esac
422 if [ "X$pidtest" = "X" ]
423 then
424 # This is a stale pid file.
425 rm -f "$PIDFILE"
426 echo "Removed stale pid file: $PIDFILE"
427 pid=""
430 else
431 echo "Cannot read $PIDFILE."
432 exit 1
437 getstatus() {
438 STATUS=
439 if [ -f "$STATUSFILE" ]
440 then
441 if [ -r "$STATUSFILE" ]
442 then
443 STATUS=`cat "$STATUSFILE"`
446 if [ "X$STATUS" = "X" ]
447 then
448 STATUS="Unknown"
451 JAVASTATUS=
452 if [ -f "$JAVASTATUSFILE" ]
453 then
454 if [ -r "$JAVASTATUSFILE" ]
455 then
456 JAVASTATUS=`cat "$JAVASTATUSFILE"`
459 if [ "X$JAVASTATUS" = "X" ]
460 then
461 JAVASTATUS="Unknown"
465 testpid() {
466 pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
467 if [ "X$pid" = "X" ]
468 then
469 # Process is gone so remove the pid file.
470 rm -f "$PIDFILE"
471 pid=""
475 console() {
476 echo "Running $APP_LONG_NAME..."
477 getpid
478 if [ "X$pid" = "X" ]
479 then
480 # The string passed to eval must handles spaces in paths correctly.
481 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"
482 eval $COMMAND_LINE
483 else
484 echo "$APP_LONG_NAME is already running."
485 exit 1
489 start() {
490 echo -n "Starting $APP_LONG_NAME..."
491 getpid
492 if [ "X$pid" = "X" ]
493 then
494 # The string passed to eval must handles spaces in paths correctly.
495 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"
496 eval $COMMAND_LINE
497 else
498 echo "$APP_LONG_NAME is already running."
499 exit 1
502 # Sleep for a few seconds to allow for intialization if required
503 # then test to make sure we're still running.
506 while [ $i -lt $WAIT_AFTER_STARTUP ]
508 sleep 1
509 echo -n "."
510 i=`expr $i + 1`
511 done
512 if [ $WAIT_AFTER_STARTUP -gt 0 ]
513 then
514 getpid
515 if [ "X$pid" = "X" ]
516 then
517 echo " WARNING: $APP_LONG_NAME may have failed to start."
518 exit 1
519 else
520 echo " running ($pid)."
522 else
523 echo ""
527 stopit() {
528 echo "Stopping $APP_LONG_NAME..."
529 getpid
530 if [ "X$pid" = "X" ]
531 then
532 echo "$APP_LONG_NAME was not running."
533 else
534 if [ "X$IGNORE_SIGNALS" = "X" ]
535 then
536 # Running so try to stop it.
537 kill $pid
538 if [ $? -ne 0 ]
539 then
540 # An explanation for the failure should have been given
541 echo "Unable to stop $APP_LONG_NAME."
542 exit 1
544 else
545 rm -f "$ANCHORFILE"
546 if [ -f "$ANCHORFILE" ]
547 then
548 # An explanation for the failure should have been given
549 echo "Unable to stop $APP_LONG_NAME."
550 exit 1
554 # We can not predict how long it will take for the wrapper to
555 # actually stop as it depends on settings in wrapper.conf.
556 # Loop until it does.
557 savepid=$pid
558 CNT=0
559 TOTCNT=0
560 while [ "X$pid" != "X" ]
562 # Show a waiting message every 5 seconds.
563 if [ "$CNT" -lt "5" ]
564 then
565 CNT=`expr $CNT + 1`
566 else
567 echo "Waiting for $APP_LONG_NAME to exit..."
568 CNT=0
570 TOTCNT=`expr $TOTCNT + 1`
572 sleep 1
574 testpid
575 done
577 pid=$savepid
578 testpid
579 if [ "X$pid" != "X" ]
580 then
581 echo "Failed to stop $APP_LONG_NAME."
582 exit 1
583 else
584 echo "Stopped $APP_LONG_NAME."
589 status() {
590 getpid
591 if [ "X$pid" = "X" ]
592 then
593 echo "$APP_LONG_NAME is not running."
594 exit 1
595 else
596 if [ "X$DETAIL_STATUS" = "X" ]
597 then
598 echo "$APP_LONG_NAME is running (PID:$pid)."
599 else
600 getstatus
601 echo "$APP_LONG_NAME is running (PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS)"
603 exit 0
607 dump() {
608 echo "Dumping $APP_LONG_NAME..."
609 getpid
610 if [ "X$pid" = "X" ]
611 then
612 echo "$APP_LONG_NAME was not running."
613 else
614 kill -3 $pid
616 if [ $? -ne 0 ]
617 then
618 echo "Failed to dump $APP_LONG_NAME."
619 exit 1
620 else
621 echo "Dumped $APP_LONG_NAME."
626 # Used by HP-UX init scripts.
627 startmsg() {
628 getpid
629 if [ "X$pid" = "X" ]
630 then
631 echo "Starting $APP_LONG_NAME... (Wrapper:Stopped)"
632 else
633 if [ "X$DETAIL_STATUS" = "X" ]
634 then
635 echo "Starting $APP_LONG_NAME... (Wrapper:Running)"
636 else
637 getstatus
638 echo "Starting $APP_LONG_NAME... (Wrapper:$STATUS, Java:$JAVASTATUS)"
643 # Used by HP-UX init scripts.
644 stopmsg() {
645 getpid
646 if [ "X$pid" = "X" ]
647 then
648 echo "Stopping $APP_LONG_NAME... (Wrapper:Stopped)"
649 else
650 if [ "X$DETAIL_STATUS" = "X" ]
651 then
652 echo "Stopping $APP_LONG_NAME... (Wrapper:Running)"
653 else
654 getstatus
655 echo "Stopping $APP_LONG_NAME... (Wrapper:$STATUS, Java:$JAVASTATUS)"
660 case "$1" in
662 'console')
663 checkUser touchlock $1
664 console
667 'start')
668 checkUser touchlock $1
669 start
672 'stop')
673 checkUser "" $1
674 stopit
677 'restart')
678 checkUser touchlock $1
679 stopit
680 start
683 'status')
684 checkUser "" $1
685 status
688 'dump')
689 checkUser "" $1
690 dump
693 'start_msg')
694 checkUser "" $1
695 startmsg
698 'stop_msg')
699 checkUser "" $1
700 stopmsg
704 echo "Usage: $0 { console | start | stop | restart | status | dump }"
705 exit 1
707 esac
709 exit 0