tom | 5c25570 | 2014-09-18 06:57:39 -0700 | [diff] [blame] | 1 | #! /bin/sh |
| 2 | |
| 3 | # ------------------------------------------------------------------------ |
| 4 | # Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | # contributor license agreements. See the NOTICE file distributed with |
| 6 | # this work for additional information regarding copyright ownership. |
| 7 | # The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | # (the "License"); you may not use this file except in compliance with |
| 9 | # the License. You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # ------------------------------------------------------------------------ |
| 19 | |
| 20 | # If require, set the JAVA_HOME to launch the wrapper |
| 21 | # |
| 22 | #JAVA_HOME= |
| 23 | # |
| 24 | |
| 25 | # Application |
| 26 | APP_NAME="onos" |
| 27 | APP_LONG_NAME="onos" |
| 28 | |
| 29 | # Wrapper |
| 30 | WRAPPER_CMD="/opt/onos/apache-karaf-3.0.1/bin/${APP_NAME}-wrapper" |
| 31 | WRAPPER_CONF="/opt/onos/apache-karaf-3.0.1/etc/${APP_NAME}-wrapper.conf" |
| 32 | |
| 33 | # Priority at which to run the wrapper. See "man nice" for valid priorities. |
| 34 | # nice is only used if a priority is specified. |
| 35 | PRIORITY= |
| 36 | |
| 37 | # Location of the data folder. |
| 38 | DATADIR="/opt/onos/apache-karaf-3.0.1/data" |
| 39 | |
| 40 | # Location of the pid file. |
| 41 | PIDDIR="/opt/onos/apache-karaf-3.0.1/data" |
| 42 | |
| 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 |
| 47 | |
| 48 | # If specified, the Wrapper will be run as the specified user. |
| 49 | # IMPORTANT - Make sure that the user has the required privileges to write |
| 50 | # the PID file and wrapper.log files. Failure to be able to write the log |
| 51 | # file will cause the Wrapper to exit without any way to write out an error |
| 52 | # message. |
| 53 | # NOTE - This will set the user which is used to run the Wrapper as well as |
| 54 | # the JVM and is not useful in situations where a privileged resource or |
| 55 | # port needs to be allocated prior to the user being changed. |
| 56 | #RUN_AS_USER= |
| 57 | |
| 58 | # The following two lines are used by the chkconfig command. Change as is |
| 59 | # appropriate for your application. They should remain commented. |
| 60 | # chkconfig: 2345 20 80 |
| 61 | # description: onos |
| 62 | |
| 63 | # Do not modify anything beyond this point |
| 64 | #----------------------------------------------------------------------------- |
| 65 | |
| 66 | # Get the fully qualified path to the script |
| 67 | case $0 in |
| 68 | /*) |
| 69 | SCRIPT="$0" |
| 70 | ;; |
| 71 | *) |
| 72 | PWD=`pwd` |
| 73 | SCRIPT="$PWD/$0" |
| 74 | ;; |
| 75 | esac |
| 76 | |
| 77 | # Resolve the true real path without any sym links. |
| 78 | CHANGED=true |
| 79 | while [ "X$CHANGED" != "X" ] |
| 80 | do |
| 81 | # Change spaces to ":" so the tokens can be parsed. |
| 82 | SCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'` |
| 83 | # Get the real path to this script, resolving any symbolic links |
| 84 | TOKENS=`echo $SCRIPT | sed -e 's;/; ;g'` |
| 85 | REALPATH= |
| 86 | for C in $TOKENS; do |
| 87 | REALPATH="$REALPATH/$C" |
| 88 | while [ -h "$REALPATH" ] ; do |
| 89 | LS="`ls -ld "$REALPATH"`" |
| 90 | LINK="`expr "$LS" : '.*-> \(.*\)$'`" |
| 91 | if expr "$LINK" : '/.*' > /dev/null; then |
| 92 | REALPATH="$LINK" |
| 93 | else |
| 94 | REALPATH="`dirname "$REALPATH"`""/$LINK" |
| 95 | fi |
| 96 | done |
| 97 | done |
| 98 | # Change ":" chars back to spaces. |
| 99 | REALPATH=`echo $REALPATH | sed -e 's;:; ;g'` |
| 100 | |
| 101 | if [ "$REALPATH" = "$SCRIPT" ] |
| 102 | then |
| 103 | CHANGED="" |
| 104 | else |
| 105 | SCRIPT="$REALPATH" |
| 106 | fi |
| 107 | done |
| 108 | |
| 109 | # Change the current directory to the location of the script |
| 110 | cd "`dirname "$REALPATH"`" |
| 111 | REALDIR=`pwd` |
| 112 | |
| 113 | # If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if |
| 114 | # the working directory is later changed. |
| 115 | FIRST_CHAR=`echo $PIDDIR | cut -c1,1` |
| 116 | if [ "$FIRST_CHAR" != "/" ] |
| 117 | then |
| 118 | PIDDIR=$REALDIR/$PIDDIR |
| 119 | fi |
| 120 | # Same test for WRAPPER_CMD |
| 121 | FIRST_CHAR=`echo $WRAPPER_CMD | cut -c1,1` |
| 122 | if [ "$FIRST_CHAR" != "/" ] |
| 123 | then |
| 124 | WRAPPER_CMD=$REALDIR/$WRAPPER_CMD |
| 125 | fi |
| 126 | # Same test for WRAPPER_CONF |
| 127 | FIRST_CHAR=`echo $WRAPPER_CONF | cut -c1,1` |
| 128 | if [ "$FIRST_CHAR" != "/" ] |
| 129 | then |
| 130 | WRAPPER_CONF=$REALDIR/$WRAPPER_CONF |
| 131 | fi |
| 132 | |
| 133 | # Process ID |
| 134 | ANCHORFILE="$PIDDIR/$APP_NAME.anchor" |
| 135 | PIDFILE="$PIDDIR/$APP_NAME.pid" |
| 136 | LOCKDIR="/var/lock/subsys" |
| 137 | LOCKFILE="$LOCKDIR/$APP_NAME" |
| 138 | pid="" |
| 139 | |
| 140 | # Resolve the location of the 'ps' command |
| 141 | PSEXE="/usr/bin/ps" |
| 142 | if [ ! -x $PSEXE ] |
| 143 | then |
| 144 | PSEXE="/bin/ps" |
| 145 | if [ ! -x $PSEXE ] |
| 146 | then |
| 147 | echo "Unable to locate 'ps'." |
| 148 | echo "Please report this message along with the location of the command on your system." |
| 149 | exit 1 |
| 150 | fi |
| 151 | fi |
| 152 | |
| 153 | # Resolve the os |
| 154 | DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]` |
| 155 | case "$DIST_OS" in |
| 156 | 'sunos') |
| 157 | DIST_OS="solaris" |
| 158 | ;; |
| 159 | 'hp-ux' | 'hp-ux64') |
| 160 | DIST_OS="hpux" |
| 161 | ;; |
| 162 | 'darwin') |
| 163 | DIST_OS="macosx" |
| 164 | ;; |
| 165 | 'unix_sv') |
| 166 | DIST_OS="unixware" |
| 167 | ;; |
| 168 | esac |
| 169 | |
| 170 | # Resolve the architecture |
| 171 | DIST_ARCH=`uname -p | tr [:upper:] [:lower:] | tr -d [:blank:]` |
| 172 | if [ "$DIST_ARCH" = "unknown" ] |
| 173 | then |
| 174 | DIST_ARCH=`uname -m | tr [:upper:] [:lower:] | tr -d [:blank:]` |
| 175 | fi |
| 176 | case "$DIST_ARCH" in |
| 177 | 'amd64' | 'ia32' | 'ia64' | 'i386' | 'i486' | 'i586' | 'i686' | 'x86_64') |
| 178 | DIST_ARCH="x86" |
| 179 | ;; |
| 180 | 'ip27') |
| 181 | DIST_ARCH="mips" |
| 182 | ;; |
| 183 | 'power' | 'powerpc' | 'power_pc' | 'ppc64') |
| 184 | DIST_ARCH="ppc" |
| 185 | ;; |
| 186 | 'pa_risc' | 'pa-risc') |
| 187 | DIST_ARCH="parisc" |
| 188 | ;; |
| 189 | 'sun4u' | 'sparcv9') |
| 190 | DIST_ARCH="sparc" |
| 191 | ;; |
| 192 | '9000/800') |
| 193 | DIST_ARCH="parisc" |
| 194 | ;; |
| 195 | esac |
| 196 | |
| 197 | # Decide on the wrapper binary to use. |
| 198 | # If a 32-bit wrapper binary exists then it will work on 32 or 64 bit |
| 199 | # platforms, if the 64-bit binary exists then the distribution most |
| 200 | # likely wants to use long names. Otherwise, look for the default. |
| 201 | # For macosx, we also want to look for universal binaries. |
| 202 | WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32" |
| 203 | if [ -x $WRAPPER_TEST_CMD ] |
| 204 | then |
| 205 | WRAPPER_CMD="$WRAPPER_TEST_CMD" |
| 206 | else |
| 207 | if [ "$DIST_OS" = "macosx" ] |
| 208 | then |
| 209 | WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-32" |
| 210 | if [ -x $WRAPPER_TEST_CMD ] |
| 211 | then |
| 212 | WRAPPER_CMD="$WRAPPER_TEST_CMD" |
| 213 | else |
| 214 | WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64" |
| 215 | if [ -x $WRAPPER_TEST_CMD ] |
| 216 | then |
| 217 | WRAPPER_CMD="$WRAPPER_TEST_CMD" |
| 218 | else |
| 219 | WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-64" |
| 220 | if [ -x $WRAPPER_TEST_CMD ] |
| 221 | then |
| 222 | WRAPPER_CMD="$WRAPPER_TEST_CMD" |
| 223 | else |
| 224 | if [ ! -x $WRAPPER_CMD ] |
| 225 | then |
| 226 | echo "Unable to locate any of the following binaries:" |
| 227 | echo " $WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32" |
| 228 | echo " $WRAPPER_CMD-$DIST_OS-universal-32" |
| 229 | echo " $WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64" |
| 230 | echo " $WRAPPER_CMD-$DIST_OS-universal-64" |
| 231 | echo " $WRAPPER_CMD" |
| 232 | exit 1 |
| 233 | fi |
| 234 | fi |
| 235 | fi |
| 236 | fi |
| 237 | else |
| 238 | WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64" |
| 239 | if [ -x $WRAPPER_TEST_CMD ] |
| 240 | then |
| 241 | WRAPPER_CMD="$WRAPPER_TEST_CMD" |
| 242 | else |
| 243 | if [ ! -x $WRAPPER_CMD ] |
| 244 | then |
| 245 | echo "Unable to locate any of the following binaries:" |
| 246 | echo " $WRAPPER_CMD-$DIST_OS-$DIST_ARCH-32" |
| 247 | echo " $WRAPPER_CMD-$DIST_OS-$DIST_ARCH-64" |
| 248 | echo " $WRAPPER_CMD" |
| 249 | exit 1 |
| 250 | fi |
| 251 | fi |
| 252 | fi |
| 253 | fi |
| 254 | |
| 255 | # Build the nice clause |
| 256 | if [ "X$PRIORITY" = "X" ] |
| 257 | then |
| 258 | CMDNICE="" |
| 259 | else |
| 260 | CMDNICE="nice -$PRIORITY" |
| 261 | fi |
| 262 | |
| 263 | # Build the anchor file clause. |
| 264 | if [ "X$IGNORE_SIGNALS" = "X" ] |
| 265 | then |
| 266 | ANCHORPROP= |
| 267 | IGNOREPROP= |
| 268 | else |
| 269 | ANCHORPROP=wrapper.anchorfile=$ANCHORFILE |
| 270 | IGNOREPROP=wrapper.ignore_signals=TRUE |
| 271 | fi |
| 272 | |
| 273 | # Build the lock file clause. Only create a lock file if the lock directory exists on this platform. |
| 274 | if [ -d $LOCKDIR ] |
| 275 | then |
| 276 | LOCKPROP=wrapper.lockfile=$LOCKFILE |
| 277 | else |
| 278 | LOCKPROP= |
| 279 | fi |
| 280 | |
| 281 | checkUser() { |
| 282 | # Check the configured user. If necessary rerun this script as the desired user. |
| 283 | if [ "X$RUN_AS_USER" != "X" ] |
| 284 | then |
| 285 | # Resolve the location of the 'id' command |
| 286 | IDEXE="/usr/xpg4/bin/id" |
| 287 | if [ ! -x $IDEXE ] |
| 288 | then |
| 289 | IDEXE="/usr/bin/id" |
| 290 | if [ ! -x $IDEXE ] |
| 291 | then |
| 292 | echo "Unable to locate 'id'." |
| 293 | echo "Please report this message along with the location of the command on your system." |
| 294 | exit 1 |
| 295 | fi |
| 296 | fi |
| 297 | |
| 298 | if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ] |
| 299 | then |
| 300 | # Already running as the configured user. Avoid password prompts by not calling su. |
| 301 | RUN_AS_USER="" |
| 302 | fi |
| 303 | fi |
| 304 | if [ "X$RUN_AS_USER" != "X" ] |
| 305 | then |
| 306 | # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be |
| 307 | # able to create the lock file. The Wrapper will be able to update this file once it |
| 308 | # is created but will not be able to delete it on shutdown. If $2 is defined then |
| 309 | # the lock file should be created for the current command |
| 310 | if [ "X$LOCKPROP" != "X" ] |
| 311 | then |
| 312 | if [ "X$2" != "X" ] |
| 313 | then |
| 314 | # Resolve the primary group |
| 315 | RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1` |
| 316 | if [ "X$RUN_AS_GROUP" = "X" ] |
| 317 | then |
| 318 | RUN_AS_GROUP=$RUN_AS_USER |
| 319 | fi |
| 320 | touch $LOCKFILE |
| 321 | chown $RUN_AS_USER:$RUN_AS_GROUP $LOCKFILE |
| 322 | fi |
| 323 | fi |
| 324 | |
| 325 | # Still want to change users, recurse. This means that the user will only be |
| 326 | # prompted for a password once. |
| 327 | su -m $RUN_AS_USER -s /bin/sh -c "$REALPATH $1" |
| 328 | RETVAL=$? |
| 329 | |
| 330 | # Now that we are the original user again, we may need to clean up the lock file. |
| 331 | if [ "X$LOCKPROP" != "X" ] |
| 332 | then |
| 333 | getpid |
| 334 | if [ "X$pid" = "X" ] |
| 335 | then |
| 336 | # Wrapper is not running so make sure the lock file is deleted. |
| 337 | if [ -f $LOCKFILE ] |
| 338 | then |
| 339 | rm $LOCKFILE |
| 340 | fi |
| 341 | fi |
| 342 | fi |
| 343 | |
| 344 | exit $RETVAL |
| 345 | fi |
| 346 | } |
| 347 | |
| 348 | getpid() { |
| 349 | if [ -f $PIDFILE ] |
| 350 | then |
| 351 | if [ -r $PIDFILE ] |
| 352 | then |
| 353 | pid=`cat $PIDFILE` |
| 354 | if [ "X$pid" != "X" ] |
| 355 | then |
| 356 | # It is possible that 'a' process with the pid exists but that it is not the |
| 357 | # correct process. This can happen in a number of cases, but the most |
| 358 | # common is during system startup after an unclean shutdown. |
| 359 | # The ps statement below looks for the specific wrapper command running as |
| 360 | # the pid. If it is not found then the pid file is considered to be stale. |
| 361 | if [ "$DIST_OS" = "solaris" ] |
| 362 | then |
| 363 | pidtest=`$PSEXE -p $pid -o comm | grep $WRAPPER_CMD | tail -1` |
| 364 | else |
| 365 | pidtest=`$PSEXE -p $pid -o command | grep $WRAPPER_CMD | tail -1` |
| 366 | fi |
| 367 | if [ "X$pidtest" = "X" ] |
| 368 | then |
| 369 | # This is a stale pid file. |
| 370 | rm -f $PIDFILE |
| 371 | echo "Removed stale pid file: $PIDFILE" |
| 372 | pid="" |
| 373 | fi |
| 374 | fi |
| 375 | else |
| 376 | echo "Cannot read $PIDFILE." |
| 377 | exit 1 |
| 378 | fi |
| 379 | fi |
| 380 | } |
| 381 | |
| 382 | testpid() { |
| 383 | pid=`$PSEXE -p $pid | grep $pid | grep -v grep | awk '{print $1}' | tail -1` |
| 384 | if [ "X$pid" = "X" ] |
| 385 | then |
| 386 | # Process is gone so remove the pid file. |
| 387 | rm -f $PIDFILE |
| 388 | pid="" |
| 389 | fi |
| 390 | } |
| 391 | |
| 392 | console() { |
| 393 | echo "Running $APP_LONG_NAME..." |
| 394 | getpid |
| 395 | if [ "X$pid" = "X" ] |
| 396 | then |
| 397 | COMMAND_LINE="$CMDNICE $WRAPPER_CMD $WRAPPER_CONF wrapper.syslog.ident=$APP_NAME wrapper.pidfile=$PIDFILE $ANCHORPROP $LOCKPROP" |
| 398 | exec $COMMAND_LINE |
| 399 | else |
| 400 | echo "$APP_LONG_NAME is already running." |
| 401 | exit 1 |
| 402 | fi |
| 403 | } |
| 404 | |
| 405 | start() { |
| 406 | echo "Starting $APP_LONG_NAME..." |
| 407 | getpid |
| 408 | if [ "X$pid" = "X" ] |
| 409 | then |
| 410 | if [ ! -d $DATADIR ]; then |
| 411 | mkdir $DATADIR |
| 412 | fi |
| 413 | if [ ! -d $DATADIR/log ]; then |
| 414 | mkdir $DATADIR/log |
| 415 | fi |
| 416 | COMMAND_LINE="$CMDNICE $WRAPPER_CMD $WRAPPER_CONF wrapper.syslog.ident=$APP_NAME wrapper.pidfile=$PIDFILE wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $LOCKPROP" |
| 417 | exec $COMMAND_LINE |
| 418 | else |
| 419 | echo "$APP_LONG_NAME is already running." |
| 420 | exit 1 |
| 421 | fi |
| 422 | } |
| 423 | |
| 424 | stopit() { |
| 425 | echo "Stopping $APP_LONG_NAME..." |
| 426 | getpid |
| 427 | if [ "X$pid" = "X" ] |
| 428 | then |
| 429 | echo "$APP_LONG_NAME was not running." |
| 430 | else |
| 431 | if [ "X$IGNORE_SIGNALS" = "X" ] |
| 432 | then |
| 433 | # Running so try to stop it. |
| 434 | kill $pid |
| 435 | if [ $? -ne 0 ] |
| 436 | then |
| 437 | # An explanation for the failure should have been given |
| 438 | echo "Unable to stop $APP_LONG_NAME." |
| 439 | exit 1 |
| 440 | fi |
| 441 | else |
| 442 | rm -f $ANCHORFILE |
| 443 | if [ -f $ANCHORFILE ] |
| 444 | then |
| 445 | # An explanation for the failure should have been given |
| 446 | echo "Unable to stop $APP_LONG_NAME." |
| 447 | exit 1 |
| 448 | fi |
| 449 | fi |
| 450 | |
| 451 | # We can not predict how long it will take for the wrapper to |
| 452 | # actually stop as it depends on settings in wrapper.conf. |
| 453 | # Loop until it does. |
| 454 | savepid=$pid |
| 455 | CNT=0 |
| 456 | TOTCNT=0 |
| 457 | while [ "X$pid" != "X" ] |
| 458 | do |
| 459 | # Show a waiting message every 5 seconds. |
| 460 | if [ "$CNT" -lt "5" ] |
| 461 | then |
| 462 | CNT=`expr $CNT + 1` |
| 463 | else |
| 464 | echo "Waiting for $APP_LONG_NAME to exit..." |
| 465 | CNT=0 |
| 466 | fi |
| 467 | TOTCNT=`expr $TOTCNT + 1` |
| 468 | |
| 469 | sleep 1 |
| 470 | |
| 471 | testpid |
| 472 | done |
| 473 | |
| 474 | pid=$savepid |
| 475 | testpid |
| 476 | if [ "X$pid" != "X" ] |
| 477 | then |
| 478 | echo "Failed to stop $APP_LONG_NAME." |
| 479 | exit 1 |
| 480 | else |
| 481 | echo "Stopped $APP_LONG_NAME." |
| 482 | fi |
| 483 | fi |
| 484 | } |
| 485 | |
| 486 | status() { |
| 487 | getpid |
| 488 | if [ "X$pid" = "X" ] |
| 489 | then |
| 490 | echo "$APP_LONG_NAME is not running." |
| 491 | exit 1 |
| 492 | else |
| 493 | echo "$APP_LONG_NAME is running ($pid)." |
| 494 | exit 0 |
| 495 | fi |
| 496 | } |
| 497 | |
| 498 | dump() { |
| 499 | echo "Dumping $APP_LONG_NAME..." |
| 500 | getpid |
| 501 | if [ "X$pid" = "X" ] |
| 502 | then |
| 503 | echo "$APP_LONG_NAME was not running." |
| 504 | |
| 505 | else |
| 506 | kill -3 $pid |
| 507 | |
| 508 | if [ $? -ne 0 ] |
| 509 | then |
| 510 | echo "Failed to dump $APP_LONG_NAME." |
| 511 | exit 1 |
| 512 | else |
| 513 | echo "Dumped $APP_LONG_NAME." |
| 514 | fi |
| 515 | fi |
| 516 | } |
| 517 | |
| 518 | case "$1" in |
| 519 | |
| 520 | 'console') |
| 521 | checkUser $1 touchlock |
| 522 | console |
| 523 | ;; |
| 524 | |
| 525 | 'start') |
| 526 | checkUser $1 touchlock |
| 527 | start |
| 528 | ;; |
| 529 | |
| 530 | 'stop') |
| 531 | checkUser $1 |
| 532 | stopit |
| 533 | ;; |
| 534 | |
| 535 | 'restart') |
| 536 | checkUser $1 touchlock |
| 537 | stopit |
| 538 | start |
| 539 | ;; |
| 540 | |
| 541 | 'status') |
| 542 | checkUser $1 |
| 543 | status |
| 544 | ;; |
| 545 | |
| 546 | 'dump') |
| 547 | checkUser $1 |
| 548 | dump |
| 549 | ;; |
| 550 | |
| 551 | *) |
| 552 | echo "Usage: $0 { console | start | stop | restart | status | dump }" |
| 553 | exit 1 |
| 554 | ;; |
| 555 | esac |
| 556 | |
| 557 | exit 0 |