karaf-service-template.init 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/sh
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. # Karaf control script
  18. # description: Karaf startup script
  19. # processname: ${KARAF_SERVICE_NAME}
  20. # pidfile: ${KARAF_SERVICE_PIDFILE}
  21. # config: ${KARAF_SERVICE_CONF}
  22. #
  23. if [ -r "${KARAF_SERVICE_CONF}" ]; then
  24. . "${KARAF_SERVICE_CONF}"
  25. else
  26. echo "Error KARAF_SERVICE_CONF not defined"
  27. exit -1
  28. fi
  29. # Location of JDK
  30. if [ -n "$JAVA_HOME" ]; then
  31. export JAVA_HOME
  32. fi
  33. # Setup the JVM
  34. if [ -z "$JAVA" ]; then
  35. if [ -n "$JAVA_HOME" ]; then
  36. JAVA="$JAVA_HOME/bin/java"
  37. else
  38. JAVA="java"
  39. fi
  40. fi
  41. if [ -z "$STARTUP_WAIT" ]; then
  42. STARTUP_WAIT=30
  43. fi
  44. if [ -z "$SHUTDOWN_WAIT" ]; then
  45. SHUTDOWN_WAIT=30
  46. fi
  47. prog=${KARAF_SERVICE_NAME}
  48. do_start() {
  49. echo "Starting $prog: "
  50. if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
  51. read ppid < "$KARAF_SERVICE_PIDFILE"
  52. if [ `ps -p $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
  53. echo "$prog is already running"
  54. return 1
  55. else
  56. rm -f "$KARAF_SERVICE_PIDFILE"
  57. fi
  58. fi
  59. LOG_PATH=`dirname "$KARAF_SERVICE_LOG"`
  60. mkdir -p "$LOG_PATH"
  61. cat /dev/null > "$KARAF_SERVICE_LOG"
  62. chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$KARAF_SERVICE_LOG"
  63. PID_PATH=`dirname "$KARAF_SERVICE_PIDFILE"`
  64. mkdir -p "$PID_PATH"
  65. chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$PID_PATH" || true
  66. if [ ! -z "$KARAF_SERVICE_USER" ]; then
  67. if [ "$KARAF_SERVICE_USER" = "root" ]; then
  68. KARAF_EXEC=exec
  69. export KARAF_EXEC
  70. JAVA_HOME=$JAVA_HOME
  71. export JAVA_HOME
  72. "$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE" daemon >> "$KARAF_SERVICE_LOG" 2>&1 &
  73. echo $! > "$KARAF_SERVICE_PIDFILE"
  74. else
  75. su - $KARAF_SERVICE_USER \
  76. -c " { export KARAF_EXEC=exec; export JAVA_HOME=$JAVA_HOME; \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" daemon >> \"$KARAF_SERVICE_LOG\" 2>&1 & } ; echo \$! >| \"$KARAF_SERVICE_PIDFILE\" "
  77. fi
  78. sleep 1
  79. if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
  80. chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$KARAF_SERVICE_PIDFILE"
  81. fi
  82. fi
  83. RETVAL=$?
  84. return $RETVAL
  85. }
  86. do_stop() {
  87. echo $"Stopping $prog: "
  88. count=0;
  89. if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
  90. read kpid < "$KARAF_SERVICE_PIDFILE"
  91. kwait=$SHUTDOWN_WAIT
  92. if [ "$KARAF_SERVICE_USER" = "root" ]; then
  93. JAVA_HOME=$JAVA_HOME
  94. export JAVA_HOME
  95. "$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE" stop >> "$KARAF_SERVICE_LOG" 2>&1
  96. else
  97. su - $KARAF_SERVICE_USER \
  98. -c "export JAVA_HOME=$JAVA_HOME; \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" stop >> \"$KARAF_SERVICE_LOG\" 2>&1"
  99. fi
  100. until [ `ps -p $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
  101. do
  102. sleep 1
  103. count=`expr $count + 1`
  104. done
  105. if [ $count -gt $kwait ]; then
  106. if [ `ps -p $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '1' ]; then
  107. kill -9 $kpid
  108. fi
  109. fi
  110. fi
  111. rm -f "$KARAF_SERVICE_PIDFILE"
  112. rm -f $KARAF_LOCKFILE
  113. }
  114. do_status() {
  115. if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
  116. read ppid < "$KARAF_SERVICE_PIDFILE"
  117. if [ `ps -p $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
  118. echo "$prog is running (pid $ppid)"
  119. return 0
  120. else
  121. echo "$prog dead but pid file exists"
  122. return 1
  123. fi
  124. fi
  125. echo "$prog is not running"
  126. return 3
  127. }
  128. case "$1" in
  129. start)
  130. do_start
  131. ;;
  132. stop)
  133. do_stop
  134. ;;
  135. restart)
  136. do_stop
  137. do_start
  138. ;;
  139. status)
  140. do_status
  141. ;;
  142. *)
  143. ## If no parameters are given, print which are avaiable.
  144. echo "Usage: $0 {start|stop|status|restart}"
  145. exit 1
  146. ;;
  147. esac