#!/bin/bash # $Id: httpd.init 481383 2009-12-22 14:30:03Z oden $ # $HeadURL: svn+ssh://svn.mandriva.com/svn/packages/cooker/apache-conf/current/SOURCES/httpd.init $ # # Startup script for the Apache Web Server # # chkconfig: 345 92 8 # description: Apache is a World Wide Web server. It is used to serve \ # HTML files and CGI. # processname: httpd # pidfile: /var/run/httpd.pid ### BEGIN INIT INFO # Provides: httpd # Required-Start: $network # Required-Stop: $network # Should-Start: mysqld postgresql radiusd ldap # Should-Stop: mysqld postgresql radiusd ldap # Default-Start: 3 4 5 # Short-Description: The Apache Web Server. # Description: Apache is a World Wide Web server. It is used to serve # HTML files and CGI. ### END INIT INFO # nuke the environment for i in `env | sed -e '/^[[:blank:]]/d' | cut -d= -f1 | grep "[A-Z]"`; do unset $i; done # Source function library. . /etc/rc.d/init.d/functions # Source the /etc/sysconfig/httpd file. if [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpd fi # Start httpd in the C locale by default. export LC_ALL=${HTTPD_LANG-"C"} export LC_CTYPE=${HTTPD_LANG-"C"} export LANG=${HTTPD_LANG-"C"} # Use a safe temp directory export TMP=${HTTPD_TMP:-"/tmp"} export TMPDIR=${HTTPD_TMPDIR:-"/tmp"} # Start with defaults httpdpid=${HTTPDPID:-"/var/run/httpd.pid"} options=${OPTIONS-""} httpd=${HTTPD:-"/usr/sbin/httpd"} statusurl=${STATUSURL:-"http://localhost/server-status"} processname="`basename $httpd`" # This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS="" ## Number of times to try to start Apache in case of error #times_start="1 2 3" times_start="1" sleep_time="1 2 3 4" # Change the major functions into functions. moduleargs() { moduleargs= for module in $1/*.so ; do if [ -x ${module} ] ; then module=`echo ${module} | awk '{ gsub(".*/",""); gsub("^mod_",""); gsub("^lib",""); gsub(".so$",""); print toupper($0)}'` moduleargs="${moduleargs} -DHAVE_$module" fi done echo ${moduleargs} } # Get the extra modules so only the main server gets them extramoduleargs() { extramoduleargs= for extramodule in $1/*.so ; do if [ -x ${extramodule} ] ; then extramodule=`echo ${extramodule} | awk '{ gsub(".*/",""); gsub("^mod_",""); gsub("^lib",""); gsub(".so$",""); print toupper($0)}'` extramoduleargs="${extramoduleargs} -DHAVE_$extramodule" fi done echo ${extramoduleargs} } # NOTE: detect/detectlib must be call before any use of $libdir ! detectlib() { local is_64bit=no case `/usr/bin/file /bin/bash` in *"ELF 64"*) is_64bit=yes ;; esac libdir="/usr/lib" case `uname -m`:$is_64bit in powerpc64:yes | s390x:yes | sparc64:yes | x86_64:yes) libdir="/usr/lib64";; esac } detect() { detectlib httpdconf=${HTTPDCONF:-"-f /etc/httpd/conf/httpd.conf"} extramodules="-DAPACHE2 `extramoduleargs /etc/httpd/extramodules`" modules=`moduleargs /etc/httpd/modules` debug=${DEBUG:-"-DNO_DETACH -DONE_PROCESS -DDEBUG"} } ## Hack for ApacheJServ that takes forever to shut down and restart snooze() { for count in 1 2 3 4 5 6 7 8 9 ; do echo "."; sleep 1; done echo } conftest() { detect badconf=0 if [ -x $httpd ]; then action "Checking configuration sanity for apache: " \ $httpd -t $httpdconf $extramodules $modules $options badconf=$? fi return $badconf } conftest_vhosts() { detect badconf=0 if [ -x $httpd ]; then action "Checking configuration sanity for apache: " \ $httpd -t $httpdconf $extramodules $modules $options -D DUMP_VHOSTS badconf=$? fi return $badconf } pidfilenuker() { if [ -z "`/sbin/pidof $processname`" ] && [ -e $httpdpid ]; then echo -n "removing stale pid file." rm -f $httpdpid echo fi } RETVAL=0 # See how we were called. case "$1" in start) if [ -n "`/sbin/pidof $processname`" ] && [ -e $httpdpid ]; then echo -n "apache is already running." echo fi pidfilenuker for tries in $times_start; do if [ -x $httpd -a ! -e $httpdpid ]; then if [ $tries -eq 1 ]; then echo -n "Starting httpd: "; fi detect if ! [ -z "${ULIMIT_OPTIONS}" ]; then ulimit ${ULIMIT_OPTIONS}; fi $httpd $httpdconf $extramodules $modules $options && success || failure RETVAL=$? echo if [ $RETVAL -eq 0 ]; then for wait in $sleep_time; do if [ ! -e $httpdpid ]; then sleep 1; fi done fi [ $RETVAL -eq 0 ] && touch /var/lock/subsys/httpd fi done ;; stop) pidfilenuker if [ -e $httpdpid ]; then echo -n "Shutting down httpd: " killproc $processname if [ -e $libdir/apache-extramodules/mod_jserv.so ]; then snooze while [ -e $httpdpid ]; do echo "Trying harder" snooze killproc $processname done fi fi RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/httpd ;; graceful-stop) detect if [ -e $httpdpid ]; then echo -n "Sending WINCH to httpd: " killproc $processname -WINCH echo fi RETVAL=$? [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/httpd ;; debug) echo -n "Starting httpd (debug mode and in the foreground): " detect if ! [ -z "${ULIMIT_OPTIONS}" ]; then ulimit ${ULIMIT_OPTIONS}; fi if [ -x /usr/bin/gdb ]; then echo "using gdb (assumes all needed debug packages are installed)" gdb $httpd --batch --quiet \ -ex "run $httpdconf $debug $extramodules $modules $options" \ -ex "thread apply all bt full" \ -ex "quit" else $httpd $httpdconf $debug \ -c "Pidfile /tmp/httpd-debug.pid" \ -c "LogLevel debug" \ -c "ErrorLog /dev/stdout" \ -c "CustomLog /dev/stdout common" \ $extramodules $modules $options fi RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/httpd /tmp/httpd-debug.pid ;; extendedstatus) detect if [ -e $httpdpid ]; then lynx -dump $statusurl | awk ' /process$/ { print; exit } { print } ' echo echo "#######################################" echo "#######################################" fi if [ ! -e $httpdpid ]; then echo "Apache is *not* running." echo RETVAL=3 fi ;; status) detect status $processname echo "Use $0 extendedstatus for more information." echo ;; restart) $0 stop $0 start RETVAL=$? ;; update|condrestart) if [ -e $httpdpid ]; then $0 stop conftest || exit $? $0 start RETVAL=$? fi ;; reload|graceful|condreload) # in doubt, check both possible library dirs if [ -e $libdir/apache-extramodules/mod_jserv.so ]; then $0 stop $0 start else detect if [ -e $httpdpid ]; then echo -n "Reloading httpd: " killproc $processname -USR1 echo fi fi ;; closelogs) detect if [ -e $httpdpid ]; then echo -n "Sending HUP to httpd: " killproc $processname -HUP echo fi ;; configtest) conftest exit $? ;; configtest_vhosts) conftest_vhosts exit $? ;; show_defines) # OE 20041013 make it spit out all those -D variables to make it # easier to use something like: # httpd -f /etc/httpd/conf/httpd.conf `/etc/rc.d/init.d/httpd show_defines` -t detect echo "$extramodules $modules $options" exit $? ;; *) echo "Usage: $0 {start|stop|graceful-stop|restart|reload|graceful|condreload|closelogs|update|condrestart|status|extendedstatus|configtest|configtest_vhosts|debug|show_defines}" exit 1 esac exit $RETVAL