PageRenderTime 9ms CodeModel.GetById 0ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/rc0.d/K09apache2

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