Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | ### BEGIN INIT INFO |
| 3 | # Provides: httpd |
| 4 | # Required-Start: $local_fs $remote_fs $network $named |
| 5 | # Required-Stop: $local_fs $remote_fs $network |
| 6 | # Should-Start: distcache |
| 7 | # Short-Description: start and stop Apache HTTP Server |
| 8 | # Description: The Apache HTTP Server is an extensible server |
| 9 | # implementing the current HTTP standards. |
| 10 | ### END INIT INFO |
| 11 | |
| 12 | ARGS="-D SSL -D PHP5 -k start" |
| 13 | NAME=apache2 |
| 14 | PATH=/bin:/usr/bin:/sbin:/usr/sbin |
| 15 | DAEMON=/usr/sbin/httpd |
| 16 | SUEXEC=/usr/lib/apache/suexec |
| 17 | PIDFILE=/run/httpd.pid |
| 18 | CONF=/etc/apache2/httpd.conf |
| 19 | APACHECTL=/usr/sbin/apachectl |
| 20 | |
| 21 | trap "" 1 |
| 22 | export LANG=C |
| 23 | export PATH |
| 24 | |
| 25 | test -f $DAEMON || exit 0 |
| 26 | test -f $APACHECTL || exit 0 |
| 27 | |
| 28 | # ensure we don't leak environment vars into apachectl |
| 29 | APACHECTL="env -i LANG=${LANG} PATH=${PATH} $APACHECTL" |
| 30 | |
| 31 | apache_conftest() { |
| 32 | if $($APACHECTL configtest > /dev/null 2>&1 ); then |
| 33 | return 0 |
| 34 | else |
| 35 | return 1 |
| 36 | fi |
| 37 | } |
| 38 | |
| 39 | apache_wait_start() { |
| 40 | local STATUS=$1 |
| 41 | |
| 42 | if [ $STATUS != 0 ] ; then |
| 43 | return $STATUS |
| 44 | fi |
| 45 | |
| 46 | local i=0 |
| 47 | while : ; do |
| 48 | PIDTMP=$(pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE)) |
| 49 | if [ -n "${PIDTMP:-}" ] && kill -0 "${PIDTMP:-}" 2> /dev/null; then |
| 50 | return $STATUS |
| 51 | fi |
| 52 | |
| 53 | if [ $i = "20" ] ; then |
| 54 | return 2 |
| 55 | fi |
| 56 | |
| 57 | sleep 1 |
| 58 | i=$(($i+1)) |
| 59 | done |
| 60 | } |
| 61 | |
| 62 | apache_wait_stop() { |
| 63 | local STATUS=$1 |
| 64 | |
| 65 | if [ $STATUS != 0 ] ; then |
| 66 | return $STATUS |
| 67 | fi |
| 68 | |
| 69 | PIDTMP=$(pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE)) |
| 70 | if [ -n "${PIDTMP:-}" ] && kill -0 "${PIDTMP:-}" 2> /dev/null; then |
| 71 | local i=0 |
| 72 | while kill -0 "${PIDTMP:-}" 2> /dev/null; do |
| 73 | if [ $i = '60' ]; then |
| 74 | STATUS=2 |
| 75 | break |
| 76 | fi |
| 77 | sleep 1 |
| 78 | i=$(($i+1)) |
| 79 | done |
| 80 | return $STATUS |
| 81 | else |
| 82 | return $STATUS |
| 83 | fi |
| 84 | } |
| 85 | |
| 86 | # |
| 87 | # Function that starts the daemon/service |
| 88 | # |
| 89 | do_start() |
| 90 | { |
| 91 | # Return |
| 92 | # 0 if daemon has been started |
| 93 | # 1 if daemon was already running |
| 94 | # 2 if daemon could not be started |
| 95 | |
| 96 | if [ -e $PIDFILE ] && pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE) > /dev/null 2>&1 ; then |
| 97 | return 1 |
| 98 | fi |
| 99 | |
| 100 | if apache_conftest ; then |
| 101 | $APACHECTL start |
| 102 | apache_wait_start $? |
| 103 | return $? |
| 104 | else |
| 105 | return 2 |
| 106 | fi |
| 107 | } |
| 108 | |
| 109 | # |
| 110 | # Function that stops the daemon/service |
| 111 | # |
| 112 | do_stop() |
| 113 | { |
| 114 | # Return |
| 115 | # 0 if daemon has been stopped |
| 116 | # 1 if daemon was already stopped |
| 117 | # 2 if daemon could not be stopped |
| 118 | # other if a failure occurred |
| 119 | |
| 120 | local AP_RET=0 |
| 121 | |
| 122 | if pidof $DAEMON > /dev/null 2>&1 ; then |
| 123 | if [ -e $PIDFILE ] && pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE) > /dev/null 2>&1 ; then |
| 124 | AP_RET=2 |
| 125 | else |
| 126 | AP_RET=1 |
| 127 | fi |
| 128 | else |
| 129 | AP_RET=0 |
| 130 | fi |
| 131 | |
| 132 | # AP_RET is: |
| 133 | # 0 if Apache (whichever) is not running |
| 134 | # 1 if Apache (whichever) is running |
| 135 | # 2 if Apache from the PIDFILE is running |
| 136 | |
| 137 | if [ $AP_RET = 0 ] ; then |
| 138 | return 1 |
| 139 | fi |
| 140 | |
| 141 | if [ $AP_RET = 2 ] && apache_conftest ; then |
| 142 | $APACHECTL stop |
| 143 | apache_wait_stop $? |
| 144 | return $? |
| 145 | else |
| 146 | if [ $AP_RET = 2 ]; then |
| 147 | kill $(pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE)) |
| 148 | apache_wait_stop $? |
| 149 | return $? |
| 150 | elif [ $AP_RET = 1 ] ; then |
| 151 | return 2 |
| 152 | fi |
| 153 | fi |
| 154 | |
| 155 | } |
| 156 | |
| 157 | case "$1" in |
| 158 | start) |
| 159 | echo -n "Starting web server: $NAME" |
| 160 | do_start |
| 161 | case $? in |
| 162 | 0|1) |
| 163 | echo . |
| 164 | exit 0 |
| 165 | ;; |
| 166 | 2) |
| 167 | echo failed |
| 168 | exit 1 |
| 169 | ;; |
| 170 | esac |
| 171 | ;; |
| 172 | |
| 173 | stop) |
| 174 | echo -n "Stopping web server: $NAME" |
| 175 | do_stop |
| 176 | case $? in |
| 177 | 0|1) |
| 178 | echo . |
| 179 | exit 0 |
| 180 | ;; |
| 181 | 2) |
| 182 | echo failed |
| 183 | exit 1 |
| 184 | ;; |
| 185 | esac |
| 186 | ;; |
| 187 | |
| 188 | reload) |
| 189 | echo -n "Reloading $NAME configuration" |
| 190 | kill -HUP `cat $PIDFILE` |
| 191 | ;; |
| 192 | |
| 193 | reload-modules) |
| 194 | echo -n "Reloading $NAME modules" |
| 195 | $APACHECTL restart |
| 196 | ;; |
| 197 | |
| 198 | restart) |
| 199 | echo "Restarting web server: $NAME" |
| 200 | do_stop |
| 201 | case "$?" in |
| 202 | 0|1) |
| 203 | do_start |
| 204 | exit $? |
| 205 | ;; |
| 206 | *) |
| 207 | # Failed to stop |
| 208 | exit 1 |
| 209 | ;; |
| 210 | esac |
| 211 | ;; |
| 212 | |
| 213 | force-reload) |
| 214 | $0 reload-modules |
| 215 | exit $? |
| 216 | ;; |
| 217 | |
| 218 | *) |
| 219 | echo "Usage: /etc/init.d/$NAME {start|stop|reload|reload-modules|force-reload|restart}" |
| 220 | exit 1 |
| 221 | ;; |
| 222 | esac |
| 223 | |
| 224 | if [ $? = 0 ]; then |
| 225 | echo . |
| 226 | exit 0 |
| 227 | else |
| 228 | echo failed |
| 229 | exit 1 |
| 230 | fi |