/rc4.d/S91apache2

http://github.com/brinkman83/bashrc · Shell · 226 lines · 183 code · 19 blank · 24 comment · 17 complexity · b2328eaddeff206c5c338c77f0b00697 MD5 · raw file

  1. #!/bin/sh -e
  2. ### BEGIN INIT INFO
  3. # Provides: apache2
  4. # Required-Start: $local_fs $remote_fs $network $syslog
  5. # Required-Stop: $local_fs $remote_fs $network $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # X-Interactive: true
  9. # Short-Description: Start/stop apache2 web server
  10. ### END INIT INFO
  11. #
  12. # apache2 This init.d script is used to start apache2.
  13. # It basically just calls apache2ctl.
  14. ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin"
  15. #[ $(ls -1 /etc/apache2/sites-enabled/ | wc -l | sed -e 's/ *//;') -eq 0 ] && \
  16. #echo "You haven't enabled any sites yet, so I'm not starting apache2." && \
  17. #echo "To add and enable a host, use addhost and enhost." && exit 0
  18. #edit /etc/default/apache2 to change this.
  19. HTCACHECLEAN_RUN=auto
  20. HTCACHECLEAN_MODE=daemon
  21. HTCACHECLEAN_SIZE=300M
  22. HTCACHECLEAN_DAEMON_INTERVAL=120
  23. HTCACHECLEAN_PATH=/var/cache/apache2/mod_disk_cache
  24. HTCACHECLEAN_OPTIONS=""
  25. set -e
  26. if [ -x /usr/sbin/apache2 ] ; then
  27. HAVE_APACHE2=1
  28. else
  29. echo "No apache MPM package installed"
  30. exit 0
  31. fi
  32. . /lib/lsb/init-functions
  33. test -f /etc/default/rcS && . /etc/default/rcS
  34. test -f /etc/default/apache2 && . /etc/default/apache2
  35. APACHE2CTL="$ENV /usr/sbin/apache2ctl"
  36. HTCACHECLEAN="$ENV /usr/sbin/htcacheclean"
  37. PIDFILE=$(. /etc/apache2/envvars && echo $APACHE_PID_FILE)
  38. if [ -z "$PIDFILE" ] ; then
  39. echo ERROR: APACHE_PID_FILE needs to be defined in /etc/apache2/envvars >&2
  40. exit 2
  41. fi
  42. check_htcacheclean() {
  43. [ "$HTCACHECLEAN_MODE" = "daemon" ] || return 1
  44. [ "$HTCACHECLEAN_RUN" = "yes" ] && return 0
  45. [ "$HTCACHECLEAN_RUN" = "auto" \
  46. -a -e /etc/apache2/mods-enabled/disk_cache.load ] && return 0
  47. return 1
  48. }
  49. start_htcacheclean() {
  50. $HTCACHECLEAN $HTCACHECLEAN_OPTIONS -d$HTCACHECLEAN_DAEMON_INTERVAL \
  51. -i -p$HTCACHECLEAN_PATH -l$HTCACHECLEAN_SIZE
  52. }
  53. stop_htcacheclean() {
  54. pkill htcacheclean 2> /dev/null || echo ...not running
  55. }
  56. pidof_apache() {
  57. # if there is actually an apache2 process whose pid is in PIDFILE,
  58. # print it and return 0.
  59. if [ -e "$PIDFILE" ]; then
  60. if pidof apache2 | tr ' ' '\n' | grep $(cat $PIDFILE); then
  61. return 0
  62. fi
  63. fi
  64. return 1
  65. }
  66. apache_stop() {
  67. if $APACHE2CTL configtest > /dev/null 2>&1; then
  68. # if the config is ok than we just stop normaly
  69. $APACHE2CTL stop 2>&1 | grep -v 'not running' >&2 || true
  70. else
  71. # if we are here something is broken and we need to try
  72. # to exit as nice and clean as possible
  73. PID=$(pidof_apache) || true
  74. if [ "${PID}" ]; then
  75. # in this case it is everything nice and dandy
  76. # and we kill apache2
  77. log_warning_msg "We failed to correctly shutdown apache, so we're now killing all running apache processes. This is almost certainly suboptimal, so please make sure your system is working as you'd expect now!"
  78. kill $PID
  79. elif [ "$(pidof apache2)" ]; then
  80. if [ "$VERBOSE" != no ]; then
  81. echo " ... failed!"
  82. echo "You may still have some apache2 processes running. There are"
  83. echo "processes named 'apache2' which do not match your pid file,"
  84. echo "and in the name of safety, we've left them alone. Please review"
  85. echo "the situation by hand."
  86. fi
  87. return 1
  88. fi
  89. fi
  90. }
  91. apache_wait_stop() {
  92. # running ?
  93. PIDTMP=$(pidof_apache) || true
  94. if kill -0 "${PIDTMP:-}" 2> /dev/null; then
  95. PID=$PIDTMP
  96. fi
  97. apache_stop
  98. # wait until really stopped
  99. if [ -n "${PID:-}" ]; then
  100. i=0
  101. while kill -0 "${PID:-}" 2> /dev/null; do
  102. if [ $i = '60' ]; then
  103. break;
  104. else
  105. if [ $i = '0' ]; then
  106. echo -n " ... waiting "
  107. else
  108. echo -n "."
  109. fi
  110. i=$(($i+1))
  111. sleep 1
  112. fi
  113. done
  114. fi
  115. }
  116. case $1 in
  117. start)
  118. log_daemon_msg "Starting web server" "apache2"
  119. if $APACHE2CTL start; then
  120. if check_htcacheclean ; then
  121. log_progress_msg htcacheclean
  122. start_htcacheclean || log_end_msg 1
  123. fi
  124. log_end_msg 0
  125. else
  126. log_end_msg 1
  127. fi
  128. ;;
  129. stop)
  130. if check_htcacheclean ; then
  131. log_daemon_msg "Stopping web server" "htcacheclean"
  132. stop_htcacheclean
  133. log_progress_msg "apache2"
  134. else
  135. log_daemon_msg "Stopping web server" "apache2"
  136. fi
  137. if apache_wait_stop; then
  138. log_end_msg 0
  139. else
  140. log_end_msg 1
  141. fi
  142. ;;
  143. graceful | reload | force-reload)
  144. if ! $APACHE2CTL configtest > /dev/null 2>&1; then
  145. $APACHE2CTL configtest || true
  146. log_end_msg 1
  147. exit 1
  148. fi
  149. log_daemon_msg "Reloading web server config" "apache2"
  150. if pidof_apache > /dev/null ; then
  151. if $APACHE2CTL graceful $2 ; then
  152. log_end_msg 0
  153. else
  154. log_end_msg 1
  155. fi
  156. fi
  157. ;;
  158. restart)
  159. if check_htcacheclean ; then
  160. log_daemon_msg "Restarting web server" "htcacheclean"
  161. stop_htcacheclean
  162. log_progress_msg apache2
  163. else
  164. log_daemon_msg "Restarting web server" "apache2"
  165. fi
  166. PID=$(pidof_apache) || true
  167. if ! apache_wait_stop; then
  168. log_end_msg 1 || true
  169. fi
  170. if $APACHE2CTL start; then
  171. if check_htcacheclean ; then
  172. start_htcacheclean || log_end_msg 1
  173. fi
  174. log_end_msg 0
  175. else
  176. log_end_msg 1
  177. fi
  178. ;;
  179. start-htcacheclean)
  180. log_daemon_msg "Starting htcacheclean"
  181. start_htcacheclean || log_end_msg 1
  182. log_end_msg 0
  183. ;;
  184. stop-htcacheclean)
  185. log_daemon_msg "Stopping htcacheclean"
  186. stop_htcacheclean
  187. log_end_msg 0
  188. ;;
  189. status)
  190. PID=$(pidof_apache) || true
  191. if [ -n "$PID" ]; then
  192. echo "Apache is running (pid $PID)."
  193. exit 0
  194. else
  195. echo "Apache is NOT running."
  196. exit 1
  197. fi
  198. ;;
  199. *)
  200. log_success_msg "Usage: /etc/init.d/apache2 {start|stop|restart|reload|force-reload|start-htcacheclean|stop-htcacheclean|status}"
  201. exit 1
  202. ;;
  203. esac