Skip to content

Commit

Permalink
apache2: wait for server to start/stop/restart
Browse files Browse the repository at this point in the history
Change start, stop, and restart functions in apache2 init script to return only
after completion (i.e. the server has started/stopped, not just received a kill
signal). Starting and stopping the server in quick sucession results in an error
because the server will attempt to stop before it has had time to start and vice
versa.

Signed-off-by: Adam Chappell <[email protected]>
Signed-off-by: Martin Jansa <[email protected]>
  • Loading branch information
achapp authored and shr-project committed Aug 31, 2015
1 parent e5abd3c commit faf070c
Showing 1 changed file with 161 additions and 4 deletions.
165 changes: 161 additions & 4 deletions meta-webserver/recipes-httpd/apache2/files/init
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,161 @@ test -f $APACHECTL || exit 0
# ensure we don't leak environment vars into apachectl
APACHECTL="env -i LANG=${LANG} PATH=${PATH} $APACHECTL"

apache_conftest() {
if $($APACHECTL configtest > /dev/null 2>&1 ); then
return 0
else
return 1
fi
}

apache_wait_start() {
local STATUS=$1

if [ $STATUS != 0 ] ; then
return $STATUS
fi

local i=0
while : ; do
PIDTMP=$(pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE))
if [ -n "${PIDTMP:-}" ] && kill -0 "${PIDTMP:-}" 2> /dev/null; then
return $STATUS
fi

if [ $i = "20" ] ; then
return 2
fi

sleep 1
i=$(($i+1))
done
}

apache_wait_stop() {
local STATUS=$1

if [ $STATUS != 0 ] ; then
return $STATUS
fi

PIDTMP=$(pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE))
if [ -n "${PIDTMP:-}" ] && kill -0 "${PIDTMP:-}" 2> /dev/null; then
local i=0
while kill -0 "${PIDTMP:-}" 2> /dev/null; do
if [ $i = '60' ]; then
STATUS=2
break
fi
sleep 1
i=$(($i+1))
done
return $STATUS
else
return $STATUS
fi
}

#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started

if [ -e $PIDFILE ] && pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE) > /dev/null 2>&1 ; then
return 1
fi

if apache_conftest ; then
$APACHECTL start
apache_wait_start $?
return $?
else
return 2
fi
}

#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred

local AP_RET=0

if pidof $DAEMON > /dev/null 2>&1 ; then
if [ -e $PIDFILE ] && pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE) > /dev/null 2>&1 ; then
AP_RET=2
else
AP_RET=1
fi
else
AP_RET=0
fi

# AP_RET is:
# 0 if Apache (whichever) is not running
# 1 if Apache (whichever) is running
# 2 if Apache from the PIDFILE is running

if [ $AP_RET = 0 ] ; then
return 1
fi

if [ $AP_RET = 2 ] && apache_conftest ; then
$APACHECTL stop
apache_wait_stop $?
return $?
else
if [ $AP_RET = 2 ]; then
kill $(pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE))
apache_wait_stop $?
return $?
elif [ $AP_RET = 1 ] ; then
return 2
fi
fi

}

case "$1" in
start)
echo -n "Starting web server: $NAME"
$APACHECTL $ARGS
do_start
case $? in
0|1)
echo .
exit 0
;;
2)
echo failed
exit 1
;;
esac
;;

stop)
$APACHECTL stop
echo -n "Stopping web server: $NAME"
do_stop
case $? in
0|1)
echo .
exit 0
;;
2)
echo failed
exit 1
;;
esac
;;

reload)
Expand All @@ -49,8 +196,18 @@ case "$1" in
;;

restart)
$APACHECTL restart
exit $?
echo "Restarting web server: $NAME"
do_stop
case "$?" in
0|1)
do_start
exit $?
;;
*)
# Failed to stop
exit 1
;;
esac
;;

force-reload)
Expand Down

0 comments on commit faf070c

Please sign in to comment.