karaf-service-template.init-debian 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. # /etc/init.d/${KARAF_SERVICE_NAME} -- startup script for Karaf
  18. #
  19. #
  20. ### BEGIN INIT INFO
  21. # Provides: ${KARAF_SERVICE_NAME}
  22. # Required-Start: $remote_fs $network
  23. # Required-Stop: $remote_fs $network
  24. # Should-Start: $named
  25. # Should-Stop: $named
  26. # Default-Start: 2 3 4 5
  27. # Default-Stop: 0 1 6
  28. # Short-Description: Karaf
  29. # Description: Provide Karaf startup/shutdown script
  30. ### END INIT INFO
  31. NAME=${KARAF_SERVICE_NAME}
  32. DESC=${KARAF_SERVICE_NAME}
  33. DEFAULT="/etc/default/$NAME"
  34. # Check privileges
  35. if [ `id -u` -ne 0 ]; then
  36. echo "You need root privileges to run this script"
  37. exit 1
  38. fi
  39. # Make sure karaf is started with system locale
  40. if [ -r /etc/default/locale ]; then
  41. . /etc/default/locale
  42. export LANG
  43. fi
  44. . /lib/lsb/init-functions
  45. if [ -r /etc/default/rcS ]; then
  46. . /etc/default/rcS
  47. fi
  48. # Overwrite settings from default file
  49. if [ -f "$DEFAULT" ]; then
  50. . "$DEFAULT"
  51. fi
  52. if [ -r "${KARAF_SERVICE_CONF}" ]; then
  53. . "${KARAF_SERVICE_CONF}"
  54. else
  55. echo "Error KARAF_SERVICE_CONF not defined"
  56. exit -1
  57. fi
  58. # Location of JDK
  59. if [ -n "$JAVA_HOME" ]; then
  60. export JAVA_HOME
  61. fi
  62. # Setup the JVM
  63. if [ -z "$JAVA" ]; then
  64. if [ -n "$JAVA_HOME" ]; then
  65. JAVA="$JAVA_HOME/bin/java"
  66. else
  67. JAVA="java"
  68. fi
  69. fi
  70. # Check karaf user
  71. id $KARAF_SERVICE_USER > /dev/null 2>&1
  72. if [ $? -ne 0 -o -z "$KARAF_SERVICE_USER" ]; then
  73. echo "User \"$KARAF_SERVICE_USER\" does not exist..." >&2
  74. exit 1
  75. fi
  76. # Check owner of KARAF_SERVICE_PATH
  77. if [ ! $(stat -L -c "%U" "$KARAF_SERVICE_PATH") = $KARAF_SERVICE_USER ]; then
  78. echo "The user \"$KARAF_SERVICE_USER\" is not owner of \"$KARAF_SERVICE_PATH\"" >&2
  79. exit 1
  80. fi
  81. # The amount of time to wait for startup
  82. if [ -z "$STARTUP_WAIT" ]; then
  83. STARTUP_WAIT=30
  84. fi
  85. # The amount of time to wait for shutdown
  86. if [ -z "$SHUTDOWN_WAIT" ]; then
  87. SHUTDOWN_WAIT=30
  88. fi
  89. # Helper function to check status of karaf service
  90. check_status() {
  91. pidofproc -p "$KARAF_SERVICE_PIDFILE" "$JAVA" >/dev/null 2>&1
  92. }
  93. case "$1" in
  94. start)
  95. echo "Starting $DESC" "$NAME"
  96. # PID file
  97. PID_PATH=$(dirname "$KARAF_SERVICE_PIDFILE")
  98. if [ ! -d "$PID_PATH" ]; then
  99. mkdir -p "$PID_PATH"
  100. fi
  101. chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$PID_PATH" || true
  102. # Console log
  103. LOG_PATH=$(dirname "$KARAF_SERVICE_LOG")
  104. if [ ! -d "$LOG_PATH" ]; then
  105. mkdir -p "$LOG_PATH"
  106. fi
  107. cat /dev/null > "$KARAF_SERVICE_LOG"
  108. chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$LOG_PATH"
  109. chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$KARAF_SERVICE_LOG"
  110. start-stop-daemon \
  111. --start \
  112. --user "$KARAF_SERVICE_USER" \
  113. --chuid "$KARAF_SERVICE_USER" \
  114. --chdir "$KARAF_SERVICE_PATH" \
  115. --pidfile "$KARAF_SERVICE_PIDFILE" \
  116. --make-pidfile \
  117. --exec "$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE" -- "daemon" \
  118. >> "$KARAF_SERVICE_LOG" 2>&1 &
  119. count=0
  120. launched=0
  121. until [ $count -gt $STARTUP_WAIT ]
  122. do
  123. sleep 1
  124. count=$((count + 1));
  125. if check_status; then
  126. launched=1
  127. break
  128. fi
  129. done
  130. if check_status; then
  131. log_end_msg 0
  132. else
  133. log_end_msg 1
  134. fi
  135. if [ $launched -eq 0 ]; then
  136. log_warning_msg "$DESC hasn't started within the timeout allowed"
  137. log_warning_msg "please review file \"$KARAF_SERVICE_LOG\" to see the status of the service"
  138. fi
  139. ;;
  140. stop)
  141. check_status
  142. status_stop=$?
  143. if [ $status_stop -eq 0 ]; then
  144. kwait=$SHUTDOWN_WAIT
  145. read kpid < "$KARAF_SERVICE_PIDFILE"
  146. log_daemon_msg "Stopping $DESC" "$NAME"
  147. children_pids=$(pgrep -P $kpid)
  148. su - $KARAF_SERVICE_USER \
  149. -c "export JAVA_HOME=$JAVA_HOME; \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" stop"
  150. count=0
  151. until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
  152. do
  153. sleep 1
  154. count=$((count + 1));
  155. done
  156. if check_status; then
  157. start-stop-daemon \
  158. --stop \
  159. --quiet \
  160. --pidfile "$KARAF_SERVICE_PIDFILE" \
  161. --user "$KARAF_SERVICE_USER" \
  162. --retry=TERM/$SHUTDOWN_WAIT/KILL/5 \
  163. > /dev/null 2>&1
  164. if [ $? -eq 2 ]; then
  165. log_failure_msg "$DESC can't be stopped"
  166. exit 1
  167. fi
  168. fi
  169. for child in $children_pids; do
  170. /bin/kill -9 $child >/dev/null 2>&1
  171. done
  172. log_end_msg 0
  173. rm -rf "$KARAF_SERVICE_PIDFILE"
  174. elif [ $status_stop -eq 1 ]; then
  175. log_action_msg "$DESC is not running but the pid file exists, cleaning up"
  176. rm -f "$KARAF_SERVICE_PIDFILE"
  177. elif [ $status_stop -eq 3 ]; then
  178. log_action_msg "$DESC is not running"
  179. fi
  180. ;;
  181. restart)
  182. check_status
  183. status_restart=$?
  184. if [ $status_restart -eq 0 ]; then
  185. $0 stop
  186. fi
  187. $0 start
  188. ;;
  189. status)
  190. check_status
  191. status=$?
  192. if [ $status -eq 0 ]; then
  193. read pid < $KARAF_SERVICE_PIDFILE
  194. log_action_msg "$DESC is running with pid $pid"
  195. exit 0
  196. elif [ $status -eq 1 ]; then
  197. log_action_msg "$DESC is not running and the pid file exists"
  198. exit 1
  199. elif [ $status -eq 3 ]; then
  200. log_action_msg "$DESC is not running"
  201. exit 3
  202. else
  203. log_action_msg "Unable to determine $DESC status"
  204. exit 4
  205. fi
  206. ;;
  207. *)
  208. log_action_msg "Usage: $0 {start|stop|restart|status}"
  209. exit 2
  210. ;;
  211. esac
  212. exit 0