edited sonar.sh to enable different java-versions on arch linux.
[archlinuxdevstack.git] / sonar / sonar.sh
blob9e21d95772e77d8e3d091c542fe3042fcbff88c4
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 # since we have a depencency to java-runtime, one of these files must
19 # exist :-)
20 if [ -f /etc/profile.d/jdk.sh ]; then
21 source /etc/profile.d/jdk.sh
23 if [ -f /etc/profile.d/jre.sh ]; then
24 source /etc/profile.d/jre.sh
26 if [ -f /etc/profile.d/openjdk.sh ]; then
27 source /etc/profile.d/openjdk.sh
30 export JAVA_HOME=${JAVA_HOME:=/opt/java}
32 export SONAR_HOME=${SONAR_HOME:=/opt/sonar}
33 export SONAR_RUN_DIR=${SONAR_RUN_DIR:=/var/run/sonar}
34 export SONAR_ETC_DIR=${SONAR_ETC_DIR:=/etc/sonar}
35 export SONAR_LOG_DIR=${SONAR_LOG_DIR:=/var/log/sonar}
37 #-----------------------------------------------------------------------------
38 # These settings can be modified to fit the needs of your application
39 # Optimized for use with version 3.3.0 of the Wrapper.
41 # These settings are highly modified for the use of the sonar application
42 # under archlinux.
44 # Application
45 APP_NAME="sonar"
46 APP_LONG_NAME="sonar"
48 # Wrapper
49 WRAPPER_CMD="$SONAR_HOME/bin/wrapper"
50 WRAPPER_CONF="$SONAR_ETC_DIR/wrapper.conf"
52 # Priority at which to run the wrapper. See "man nice" for valid priorities.
53 # nice is only used if a priority is specified.
54 PRIORITY=
56 # Location of the pid file.
57 PIDDIR="$SONAR_RUN_DIR"
59 # If uncommented, causes the Wrapper to be shutdown using an anchor file.
60 # When launched with the 'start' command, it will also ignore all INT and
61 # TERM signals.
62 #IGNORE_SIGNALS=true
64 # Wrapper will start the JVM asynchronously. Your application may have some
65 # initialization tasks and it may be desirable to wait a few seconds
66 # before returning. For example, to delay the invocation of following
67 # startup scripts. Setting WAIT_AFTER_STARTUP to a positive number will
68 # cause the start command to delay for the indicated period of time
69 # (in seconds).
71 WAIT_AFTER_STARTUP=0
73 # If set, the status, start_msg and stop_msg commands will print out detailed
74 # state information on the Wrapper and Java processes.
75 DETAIL_STATUS=true
77 # If specified, the Wrapper will be run as the specified user.
78 # IMPORTANT - Make sure that the user has the required privileges to write
79 # the PID file and wrapper.log files. Failure to be able to write the log
80 # file will cause the Wrapper to exit without any way to write out an error
81 # message.
82 # NOTE - This will set the user which is used to run the Wrapper as well as
83 # the JVM and is not useful in situations where a privileged resource or
84 # port needs to be allocated prior to the user being changed.
85 RUN_AS_USER=sonar
87 # The following two lines are used by the chkconfig command. Change as is
88 # appropriate for your application. They should remain commented.
89 # chkconfig: 2345 20 80
90 # description: "Sonar Server"
92 # Initialization block for the install_initd and remove_initd scripts used by
93 # SUSE linux distributions.
94 ### BEGIN INIT INFO
95 # Provides: @app.name@
96 # Required-Start: $local_fs $network $syslog
97 # Should-Start:
98 # Required-Stop:
99 # Default-Start: 2 3 4 5
100 # Default-Stop: 0 1 6
101 # Short-Description: @app.long.name@
102 # Description: @app.description@
103 ### END INIT INFO
105 # Do not modify anything beyond this point
106 #-----------------------------------------------------------------------------
108 # Get the fully qualified path to the script
109 case $0 in
111 SCRIPT="$0"
114 PWD=`pwd`
115 SCRIPT="$PWD/$0"
117 esac
119 # Resolve the true real path without any sym links.
120 CHANGED=true
121 while [ "X$CHANGED" != "X" ]
123 # Change spaces to ":" so the tokens can be parsed.
124 SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
125 # Get the real path to this script, resolving any symbolic links
126 TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
127 REALPATH=
128 for C in $TOKENS; do
129 # Change any ":" in the token back to a space.
130 C=`echo $C | sed -e 's;:; ;g'`
131 REALPATH="$REALPATH/$C"
132 # If REALPATH is a sym link, resolve it. Loop for nested links.
133 while [ -h "$REALPATH" ] ; do
134 LS="`ls -ld "$REALPATH"`"
135 LINK="`expr "$LS" : '.*-> \(.*\)$'`"
136 if expr "$LINK" : '/.*' > /dev/null; then
137 # LINK is absolute.
138 REALPATH="$LINK"
139 else
140 # LINK is relative.
141 REALPATH="`dirname "$REALPATH"`""/$LINK"
143 done
144 done
146 if [ "$REALPATH" = "$SCRIPT" ]
147 then
148 CHANGED=""
149 else
150 SCRIPT="$REALPATH"
152 done
154 # Change the current directory to the location of the script
155 cd "`dirname "$REALPATH"`"
156 REALDIR=`pwd`
158 # If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
159 # the working directory is later changed.
160 FIRST_CHAR=`echo $PIDDIR | cut -c1,1`
161 if [ "$FIRST_CHAR" != "/" ]
162 then
163 PIDDIR=$REALDIR/$PIDDIR
165 # Same test for WRAPPER_CMD
166 FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1`
167 if [ "$FIRST_CHAR" != "/" ]
168 then
169 WRAPPER_CMD=$REALDIR/$WRAPPER_CMD
171 # Same test for WRAPPER_CONF
172 FIRST_CHAR=`echo $WRAPPER_CONF | cut -c1,1`
173 if [ "$FIRST_CHAR" != "/" ]
174 then
175 WRAPPER_CONF=$REALDIR/$WRAPPER_CONF
178 # Process ID
179 ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
180 STATUSFILE="$PIDDIR/$APP_NAME.status"
181 JAVASTATUSFILE="$PIDDIR/$APP_NAME.java.status"
182 PIDFILE="$PIDDIR/$APP_NAME.pid"
183 LOCKDIR="/var/lock/subsys"
184 LOCKFILE="$LOCKDIR/$APP_NAME"
185 pid=""
187 # Resolve the location of the 'ps' command
188 PSEXE="/usr/bin/ps"
189 if [ ! -x "$PSEXE" ]
190 then
191 PSEXE="/bin/ps"
192 if [ ! -x "$PSEXE" ]
193 then
194 echo "Unable to locate 'ps'."
195 echo "Please report this message along with the location of the command on your system."
196 exit 1
200 # Resolve the os
201 DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
202 case "$DIST_OS" in
203 'sunos')
204 DIST_OS="solaris"
206 'hp-ux' | 'hp-ux64')
207 # HP-UX needs the XPG4 version of ps (for -o args)
208 DIST_OS="hpux"
209 UNIX95=""
210 export UNIX95
212 'darwin')
213 DIST_OS="macosx"
215 'unix_sv')
216 DIST_OS="unixware"
218 esac
220 # Resolve the architecture
221 if [ "$DIST_OS" = "macosx" ]
222 then
223 DIST_ARCH="universal"
224 else
225 DIST_ARCH=
226 DIST_ARCH=`uname -p 2>/dev/null | tr [:upper:] [:lower:] | tr -d [:blank:]`
227 if [ "X$DIST_ARCH" = "X" ]
228 then
229 DIST_ARCH="unknown"
231 if [ "$DIST_ARCH" = "unknown" ]
232 then
233 DIST_ARCH=`uname -m 2>/dev/null | tr [:upper:] [:lower:] | tr -d [:blank:]`
235 case "$DIST_ARCH" in
236 'amd64' | 'athlon' | 'i386' | 'i486' | 'i586' | 'i686' | 'x86_64')
237 DIST_ARCH="x86"
239 'ia32' | 'ia64' | 'ia64n' | 'ia64w')
240 DIST_ARCH="ia"
242 'ip27')
243 DIST_ARCH="mips"
245 'power' | 'powerpc' | 'power_pc' | 'ppc64')
246 DIST_ARCH="ppc"
248 'pa_risc' | 'pa-risc')
249 DIST_ARCH="parisc"
251 'sun4u' | 'sparcv9')
252 DIST_ARCH="sparc"
254 '9000/800')
255 DIST_ARCH="parisc"
257 esac
260 # OSX always places Java in the same location so we can reliably set JAVA_HOME
261 if [ "$DIST_OS" = "macosx" ]
262 then
263 if [ -z "$JAVA_HOME" ]; then
264 JAVA_HOME="/Library/Java/Home"; export JAVA_HOME
268 outputFile() {
269 if [ -f "$1" ]
270 then
271 echo " $1 (Found but not executable.)";
272 else
273 echo " $1"
277 # Decide on the wrapper binary to use.
278 # If a 32-bit wrapper binary exists then it will work on 32 or 64 bit
279 # platforms, if the 64-bit binary exists then the distribution most
280 # likely wants to use long names. Otherwise, look for the default.
281 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
282 if [ -x "$WRAPPER_TEST_CMD" ]
283 then
284 WRAPPER_CMD="$WRAPPER_TEST_CMD"
285 else
286 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
287 if [ -x "$WRAPPER_TEST_CMD" ]
288 then
289 WRAPPER_CMD="$WRAPPER_TEST_CMD"
290 else
291 if [ ! -x "$WRAPPER_CMD" ]
292 then
293 echo "Unable to locate any of the following binaries:"
294 outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32"
295 outputFile "$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64"
296 outputFile "$WRAPPER_CMD"
297 exit 1
302 # Build the nice clause
303 if [ "X$PRIORITY" = "X" ]
304 then
305 CMDNICE=""
306 else
307 CMDNICE="nice -$PRIORITY"
310 # Build the anchor file clause.
311 if [ "X$IGNORE_SIGNALS" = "X" ]
312 then
313 ANCHORPROP=
314 IGNOREPROP=
315 else
316 ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
317 IGNOREPROP=wrapper.ignore_signals=TRUE
320 # Build the status file clause.
321 if [ "X$DETAIL_STATUS" = "X" ]
322 then
323 STATUSPROP=
324 else
325 STATUSPROP="wrapper.statusfile=\"$STATUSFILE\" wrapper.java.statusfile=\"$JAVASTATUSFILE\""
328 # Build the lock file clause. Only create a lock file if the lock directory exists on this platform.
329 LOCKPROP=
330 if [ -d $LOCKDIR ]
331 then
332 if [ -w $LOCKDIR ]
333 then
334 LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
338 checkUser() {
339 # $1 touchLock flag
340 # $2 command
342 # Check the configured user. If necessary rerun this script as the desired user.
343 if [ "X$RUN_AS_USER" != "X" ]
344 then
345 # Resolve the location of the 'id' command
346 IDEXE="/usr/xpg4/bin/id"
347 if [ ! -x "$IDEXE" ]
348 then
349 IDEXE="/usr/bin/id"
350 if [ ! -x "$IDEXE" ]
351 then
352 echo "Unable to locate 'id'."
353 echo "Please report this message along with the location of the command on your system."
354 exit 1
358 if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
359 then
360 # Already running as the configured user. Avoid password prompts by not calling su.
361 RUN_AS_USER=""
364 if [ "X$RUN_AS_USER" != "X" ]
365 then
366 # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
367 # able to create the lock file. The Wrapper will be able to update this file once it
368 # is created but will not be able to delete it on shutdown. If $2 is defined then
369 # the lock file should be created for the current command
370 if [ "X$LOCKPROP" != "X" ]
371 then
372 if [ "X$1" != "X" ]
373 then
374 # Resolve the primary group
375 RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
376 if [ "X$RUN_AS_GROUP" = "X" ]
377 then
378 RUN_AS_GROUP=$RUN_AS_USER
380 touch $LOCKFILE
381 chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE
385 # Still want to change users, recurse. This means that the user will only be
386 # prompted for a password once. Variables shifted by 1
388 # Use "runuser" if this exists. runuser should be used on RedHat in preference to su.
390 if test -f "/sbin/runuser"
391 then
392 /sbin/runuser - $RUN_AS_USER -c "\"$REALPATH\" $2"
393 else
394 su - $RUN_AS_USER -c "\"$REALPATH\" $2"
397 # Now that we are the original user again, we may need to clean up the lock file.
398 if [ "X$LOCKPROP" != "X" ]
399 then
400 getpid
401 if [ "X$pid" = "X" ]
402 then
403 # Wrapper is not running so make sure the lock file is deleted.
404 if [ -f "$LOCKFILE" ]
405 then
406 rm "$LOCKFILE"
411 exit 0
415 getpid() {
416 pid=""
417 if [ -f "$PIDFILE" ]
418 then
419 if [ -r "$PIDFILE" ]
420 then
421 pid=`cat "$PIDFILE"`
422 if [ "X$pid" != "X" ]
423 then
424 # It is possible that 'a' process with the pid exists but that it is not the
425 # correct process. This can happen in a number of cases, but the most
426 # common is during system startup after an unclean shutdown.
427 # The ps statement below looks for the specific wrapper command running as
428 # the pid. If it is not found then the pid file is considered to be stale.
429 case "$DIST_OS" in
430 'macosx')
431 pidtest=`$PSEXE -ww -p $pid -o command | grep "$WRAPPER_CMD" | tail -1`
434 pidtest=`$PSEXE -p $pid -o args | grep "$WRAPPER_CMD" | tail -1`
436 esac
438 if [ "X$pidtest" = "X" ]
439 then
440 # This is a stale pid file.
441 rm -f "$PIDFILE"
442 echo "Removed stale pid file: $PIDFILE"
443 pid=""
446 else
447 echo "Cannot read $PIDFILE."
448 exit 1
453 getstatus() {
454 STATUS=
455 if [ -f "$STATUSFILE" ]
456 then
457 if [ -r "$STATUSFILE" ]
458 then
459 STATUS=`cat "$STATUSFILE"`
462 if [ "X$STATUS" = "X" ]
463 then
464 STATUS="Unknown"
467 JAVASTATUS=
468 if [ -f "$JAVASTATUSFILE" ]
469 then
470 if [ -r "$JAVASTATUSFILE" ]
471 then
472 JAVASTATUS=`cat "$JAVASTATUSFILE"`
475 if [ "X$JAVASTATUS" = "X" ]
476 then
477 JAVASTATUS="Unknown"
481 testpid() {
482 pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1`
483 if [ "X$pid" = "X" ]
484 then
485 # Process is gone so remove the pid file.
486 rm -f "$PIDFILE"
487 pid=""
491 console() {
492 echo "Running $APP_LONG_NAME..."
493 getpid
494 if [ "X$pid" = "X" ]
495 then
496 # The string passed to eval must handles spaces in paths correctly.
497 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"
498 eval $COMMAND_LINE
499 else
500 echo "$APP_LONG_NAME is already running."
501 exit 1
505 start() {
506 echo -n "Starting $APP_LONG_NAME..."
507 getpid
508 if [ "X$pid" = "X" ]
509 then
510 # The string passed to eval must handles spaces in paths correctly.
511 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"
512 eval $COMMAND_LINE
513 else
514 echo "$APP_LONG_NAME is already running."
515 exit 1
518 # Sleep for a few seconds to allow for intialization if required
519 # then test to make sure we're still running.
522 while [ $i -lt $WAIT_AFTER_STARTUP ]
524 sleep 1
525 echo -n "."
526 i=`expr $i + 1`
527 done
528 if [ $WAIT_AFTER_STARTUP -gt 0 ]
529 then
530 getpid
531 if [ "X$pid" = "X" ]
532 then
533 echo " WARNING: $APP_LONG_NAME may have failed to start."
534 exit 1
535 else
536 echo " running ($pid)."
538 else
539 echo ""
543 stopit() {
544 echo "Stopping $APP_LONG_NAME..."
545 getpid
546 if [ "X$pid" = "X" ]
547 then
548 echo "$APP_LONG_NAME was not running."
549 else
550 if [ "X$IGNORE_SIGNALS" = "X" ]
551 then
552 # Running so try to stop it.
553 kill $pid
554 if [ $? -ne 0 ]
555 then
556 # An explanation for the failure should have been given
557 echo "Unable to stop $APP_LONG_NAME."
558 exit 1
560 else
561 rm -f "$ANCHORFILE"
562 if [ -f "$ANCHORFILE" ]
563 then
564 # An explanation for the failure should have been given
565 echo "Unable to stop $APP_LONG_NAME."
566 exit 1
570 # We can not predict how long it will take for the wrapper to
571 # actually stop as it depends on settings in wrapper.conf.
572 # Loop until it does.
573 savepid=$pid
574 CNT=0
575 TOTCNT=0
576 while [ "X$pid" != "X" ]
578 # Show a waiting message every 5 seconds.
579 if [ "$CNT" -lt "5" ]
580 then
581 CNT=`expr $CNT + 1`
582 else
583 echo "Waiting for $APP_LONG_NAME to exit..."
584 CNT=0
586 TOTCNT=`expr $TOTCNT + 1`
588 sleep 1
590 testpid
591 done
593 pid=$savepid
594 testpid
595 if [ "X$pid" != "X" ]
596 then
597 echo "Failed to stop $APP_LONG_NAME."
598 exit 1
599 else
600 echo "Stopped $APP_LONG_NAME."
605 status() {
606 getpid
607 if [ "X$pid" = "X" ]
608 then
609 echo "$APP_LONG_NAME is not running."
610 exit 1
611 else
612 if [ "X$DETAIL_STATUS" = "X" ]
613 then
614 echo "$APP_LONG_NAME is running (PID:$pid)."
615 else
616 getstatus
617 echo "$APP_LONG_NAME is running (PID:$pid, Wrapper:$STATUS, Java:$JAVASTATUS)"
619 exit 0
623 dump() {
624 echo "Dumping $APP_LONG_NAME..."
625 getpid
626 if [ "X$pid" = "X" ]
627 then
628 echo "$APP_LONG_NAME was not running."
629 else
630 kill -3 $pid
632 if [ $? -ne 0 ]
633 then
634 echo "Failed to dump $APP_LONG_NAME."
635 exit 1
636 else
637 echo "Dumped $APP_LONG_NAME."
642 # Used by HP-UX init scripts.
643 startmsg() {
644 getpid
645 if [ "X$pid" = "X" ]
646 then
647 echo "Starting $APP_LONG_NAME... (Wrapper:Stopped)"
648 else
649 if [ "X$DETAIL_STATUS" = "X" ]
650 then
651 echo "Starting $APP_LONG_NAME... (Wrapper:Running)"
652 else
653 getstatus
654 echo "Starting $APP_LONG_NAME... (Wrapper:$STATUS, Java:$JAVASTATUS)"
659 # Used by HP-UX init scripts.
660 stopmsg() {
661 getpid
662 if [ "X$pid" = "X" ]
663 then
664 echo "Stopping $APP_LONG_NAME... (Wrapper:Stopped)"
665 else
666 if [ "X$DETAIL_STATUS" = "X" ]
667 then
668 echo "Stopping $APP_LONG_NAME... (Wrapper:Running)"
669 else
670 getstatus
671 echo "Stopping $APP_LONG_NAME... (Wrapper:$STATUS, Java:$JAVASTATUS)"
676 case "$1" in
678 'console')
679 checkUser touchlock $1
680 console
683 'start')
684 checkUser touchlock $1
685 start
688 'stop')
689 checkUser "" $1
690 stopit
693 'restart')
694 checkUser touchlock $1
695 stopit
696 start
699 'status')
700 checkUser "" $1
701 status
704 'dump')
705 checkUser "" $1
706 dump
709 'start_msg')
710 checkUser "" $1
711 startmsg
714 'stop_msg')
715 checkUser "" $1
716 stopmsg
720 echo "Usage: $0 { console | start | stop | restart | status | dump }"
721 exit 1
723 esac
725 exit 0