karaf-service-template.init-redhat 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #
  19. # chkconfig: - 80 20
  20. # description: Karaf startup script
  21. # processname: ${KARAF_SERVICE_NAME}
  22. # pidfile: ${KARAF_SERVICE_PIDFILE}
  23. # config: ${KARAF_SERVICE_CONF}
  24. #
  25. # Source function library.
  26. . /etc/init.d/functions
  27. # Load Java configuration.
  28. [ -r /etc/java/java.conf ] && . /etc/java/java.conf
  29. export JAVA_HOME
  30. if [ -r "${KARAF_SERVICE_CONF}" ]; then
  31. . "${KARAF_SERVICE_CONF}"
  32. else
  33. echo "Error KARAF_SERVICE_CONF not defined"
  34. exit -1
  35. fi
  36. if [ -z "$STARTUP_WAIT" ]; then
  37. STARTUP_WAIT=30
  38. fi
  39. if [ -z "$SHUTDOWN_WAIT" ]; then
  40. SHUTDOWN_WAIT=30
  41. fi
  42. prog=${KARAF_SERVICE_NAME}
  43. currenttime=$(date +%s%N | cut -b1-13)
  44. start() {
  45. echo -n "Starting $prog: "
  46. if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
  47. read ppid < "$KARAF_SERVICE_PIDFILE"
  48. if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
  49. echo -n "$prog is already running"
  50. failure
  51. echo
  52. return 1
  53. else
  54. rm -f "$KARAF_SERVICE_PIDFILE"
  55. fi
  56. fi
  57. LOG_PATH=`dirname "$KARAF_SERVICE_LOG"`
  58. mkdir -p "$LOG_PATH"
  59. cat /dev/null > "$KARAF_SERVICE_LOG"
  60. chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$KARAF_SERVICE_LOG"
  61. PID_PATH=`dirname "$KARAF_SERVICE_PIDFILE"`
  62. mkdir -p "$PID_PATH"
  63. chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$PID_PATH" || true
  64. if [ ! -z "$KARAF_SERVICE_USER" ]; then
  65. if [ -r /etc/rc.d/init.d/functions ]; then
  66. daemon \
  67. --user="$KARAF_SERVICE_USER" \
  68. --pidfile="$KARAF_SERVICE_PIDFILE" \
  69. " { \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" daemon >> \"$KARAF_SERVICE_LOG\" 2>&1 & } ; echo \$! >| \"$KARAF_SERVICE_PIDFILE\" "
  70. else
  71. su - $KARAF_SERVICE_USER \
  72. -c " { \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" daemon >> \"$KARAF_SERVICE_LOG\" 2>&1 & } ; echo \$! >| \"$KARAF_SERVICE_PIDFILE\" "
  73. fi
  74. if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
  75. chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$KARAF_SERVICE_PIDFILE"
  76. fi
  77. fi
  78. RETVAL=$?
  79. echo
  80. if [ $RETVAL -eq 0 ]; then
  81. touch $KARAF_LOCKFILE
  82. fi
  83. return $RETVAL
  84. }
  85. stop() {
  86. echo -n $"Stopping $prog: "
  87. count=0;
  88. if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
  89. read kpid < "$KARAF_SERVICE_PIDFILE"
  90. let kwait=$SHUTDOWN_WAIT
  91. # Try issuing SIGTERM
  92. su - $KARAF_SERVICE_USER \
  93. -c "export JAVA_HOME=$JAVA_HOME; \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" stop"
  94. until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
  95. do
  96. sleep 1
  97. let count=$count+1;
  98. done
  99. if [ $count -gt $kwait ]; then
  100. kill -9 $kpid
  101. fi
  102. fi
  103. rm -f "$KARAF_SERVICE_PIDFILE"
  104. rm -f "$KARAF_LOCKFILE"
  105. success
  106. echo
  107. }
  108. status() {
  109. if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
  110. read ppid < "$KARAF_SERVICE_PIDFILE"
  111. if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
  112. echo "$prog is running (pid $ppid)"
  113. return 0
  114. else
  115. echo "$prog dead but pid file exists"
  116. return 1
  117. fi
  118. fi
  119. echo "$prog is not running"
  120. return 3
  121. }
  122. case "$1" in
  123. start)
  124. start
  125. ;;
  126. stop)
  127. stop
  128. ;;
  129. restart)
  130. $0 stop
  131. $0 start
  132. ;;
  133. status)
  134. status
  135. ;;
  136. *)
  137. ## If no parameters are given, print which are avaiable.
  138. echo "Usage: $0 {start|stop|status|restart}"
  139. exit 1
  140. ;;
  141. esac