Advertisement
clementdugal

/etc/init.d/apache2 (new server)

Oct 4th, 2014
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.13 KB | None | 0 0
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:          apache2
  4. # Required-Start:    $local_fs $remote_fs $network $syslog $named
  5. # Required-Stop:     $local_fs $remote_fs $network $syslog $named
  6. # Default-Start:     2 3 4 5
  7. # Default-Stop:      0 1 6
  8. # X-Interactive:     true
  9. # Short-Description: Apache2 web server
  10. # Description:       Start the web server and associated helpers
  11. #  This script will start apache2, and possibly all associated instances.
  12. #  Moreover, it will set-up temporary directories and helper tools such as
  13. #  htcacheclean when required by the configuration.
  14. ### END INIT INFO
  15.  
  16. DESC="web server"
  17. NAME=apache2
  18. DAEMON=/usr/sbin/$NAME
  19.  
  20. SCRIPTNAME="${0##*/}"
  21. SCRIPTNAME="${SCRIPTNAME##[KS][0-9][0-9]}"
  22. if [ -n "$APACHE_CONFDIR" ] ; then
  23.     if [ "${APACHE_CONFDIR##/etc/apache2-}" != "${APACHE_CONFDIR}" ] ; then
  24.             DIR_SUFFIX="${APACHE_CONFDIR##/etc/apache2-}"
  25.     else
  26.             DIR_SUFFIX=
  27.     fi
  28. elif [ "${SCRIPTNAME##apache2-}" != "$SCRIPTNAME" ] ; then
  29.     DIR_SUFFIX="-${SCRIPTNAME##apache2-}"
  30.     APACHE_CONFDIR=/etc/apache2$DIR_SUFFIX
  31. else
  32.     DIR_SUFFIX=
  33.     APACHE_CONFDIR=/etc/apache2
  34. fi
  35. if [ -z "$APACHE_ENVVARS" ] ; then
  36.     APACHE_ENVVARS=$APACHE_CONFDIR/envvars
  37. fi
  38. export APACHE_CONFDIR APACHE_ENVVARS
  39.  
  40. ENV="env -i LANG=C PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  41. if [ "$APACHE_CONFDIR" != /etc/apache2 ] ; then
  42.     ENV="$ENV APACHE_CONFDIR=$APACHE_CONFDIR"
  43. fi
  44. if [ "$APACHE_ENVVARS" != "$APACHE_CONFDIR/envvars" ] ; then
  45.     ENV="$ENV APACHE_ENVVARS=$APACHE_ENVVARS"
  46. fi
  47.  
  48.  
  49. #edit /etc/default/apache2 to change this.
  50. HTCACHECLEAN_RUN=auto
  51. HTCACHECLEAN_MODE=daemon
  52. HTCACHECLEAN_SIZE=300M
  53. HTCACHECLEAN_DAEMON_INTERVAL=120
  54. HTCACHECLEAN_PATH=/var/cache/apache2$DIR_SUFFIX/mod_cache_disk
  55. HTCACHECLEAN_OPTIONS=""
  56.  
  57. # Read configuration variable file if it is present
  58. if [ -f /etc/default/apache2$DIR_SUFFIX ] ; then
  59.     . /etc/default/apache2$DIR_SUFFIX
  60. elif [ -f /etc/default/apache2 ] ; then
  61.     . /etc/default/apache2
  62. fi
  63.  
  64. PIDFILE=$(. $APACHE_ENVVARS && echo $APACHE_PID_FILE)
  65.  
  66. VERBOSE=no
  67. if [ -f /etc/default/rcS ]; then
  68.     . /etc/default/rcS
  69. fi
  70. . /lib/lsb/init-functions
  71.  
  72.  
  73. # Now, set defaults:
  74. APACHE2CTL="$ENV apache2ctl"
  75. HTCACHECLEAN="$ENV htcacheclean"
  76. PIDFILE=$(. $APACHE_ENVVARS && echo $APACHE_PID_FILE)
  77. APACHE2_INIT_MESSAGE=""
  78.  
  79. CONFTEST_OUTFILE=
  80. cleanup() {
  81.     if [ -n "$CONFTEST_OUTFILE" ] ; then
  82.         rm -f "$CONFTEST_OUTFILE"
  83.     fi
  84. }
  85. trap cleanup 0  # "0" means "EXIT", but "EXIT" is not portable
  86.  
  87.  
  88. apache_conftest() {
  89.     [ -z "$CONFTEST_OUTFILE" ] || rm -f "$CONFTEST_OUTFILE"
  90.     CONFTEST_OUTFILE=$(mktemp)
  91.     if ! $APACHE2CTL configtest > "$CONFTEST_OUTFILE" 2>&1 ; then
  92.         return 1
  93.     else
  94.         rm -f "$CONFTEST_OUTFILE"
  95.         CONFTEST_OUTFILE=
  96.         return 0
  97.     fi
  98. }
  99.  
  100. clear_error_msg() {
  101.     [ -z "$CONFTEST_OUTFILE" ] || rm -f "$CONFTEST_OUTFILE"
  102.     CONFTEST_OUTFILE=
  103.     APACHE2_INIT_MESSAGE=
  104. }
  105.  
  106. print_error_msg() {
  107.     [ -z "$APACHE2_INIT_MESSAGE" ] || log_warning_msg "$APACHE2_INIT_MESSAGE"
  108.     if [ -n "$CONFTEST_OUTFILE" ] ; then
  109.         echo "Output of config test was:" >&2
  110.         cat "$CONFTEST_OUTFILE" >&2
  111.         rm -f "$CONFTEST_OUTFILE"
  112.         CONFTEST_OUTFILE=
  113.     fi
  114. }
  115.  
  116. apache_wait_start() {
  117.     local STATUS=$1
  118.     local i=0
  119.     while : ; do
  120.             PIDTMP=$(pidofproc -p $PIDFILE $DAEMON)
  121.             if [ -n "${PIDTMP:-}" ] && kill -0 "${PIDTMP:-}" 2> /dev/null; then
  122.                     return $STATUS
  123.             fi
  124.  
  125.             if [ $i = "20" ] ; then
  126.                     APACHE2_INIT_MESSAGE="The apache2$DIR_SUFFIX instance did not start within 20 seconds. Please read the log files to discover problems"
  127.                     return 2
  128.             fi
  129.  
  130.             [ "$VERBOSE" != no ] && log_progress_msg "."
  131.             sleep 1
  132.             i=$(($i+1))
  133.     done
  134. }
  135.  
  136. apache_wait_stop() {
  137.     local STATUS=$1
  138.  
  139.     PIDTMP=$(pidofproc -p $PIDFILE $DAEMON)
  140.     if [ -n "${PIDTMP:-}" ] && kill -0 "${PIDTMP:-}" 2> /dev/null; then
  141.             local i=0
  142.             while kill -0 "${PIDTMP:-}" 2> /dev/null;  do
  143.                     if [ $i = '60' ]; then
  144.                             break
  145.                             STATUS=2
  146.                     fi
  147.                     [ "$VERBOSE" != no ] && log_progress_msg "."
  148.                     sleep 1
  149.                     i=$(($i+1))
  150.             done
  151.             return $STATUS
  152.     else
  153.         return $STATUS
  154.     fi
  155. }
  156.  
  157.  
  158. #
  159. # Function that starts the daemon/service
  160. #
  161. do_start()
  162. {
  163.     # Return
  164.     #   0 if daemon has been started
  165.     #   1 if daemon was already running
  166.     #   2 if daemon could not be started
  167.  
  168.     if pidofproc -p $PIDFILE "$DAEMON" > /dev/null 2>&1 ; then
  169.             return 1
  170.     fi
  171.  
  172.     if apache_conftest ; then
  173.             $APACHE2CTL start
  174.             apache_wait_start $?
  175.             return $?
  176.     else
  177.             APACHE2_INIT_MESSAGE="The apache2$DIR_SUFFIX configtest failed."
  178.             return 2
  179.     fi
  180. }
  181.  
  182. #
  183. # Function that stops the daemon/service
  184. #
  185. do_stop()
  186. {
  187.     # Return
  188.     #   0 if daemon has been stopped
  189.     #   1 if daemon was already stopped
  190.     #   2 if daemon could not be stopped
  191.     #   other if a failure occurred
  192.  
  193.     # either "stop" or "graceful-stop"
  194.     local STOP=$1
  195.     # can't use pidofproc from LSB here
  196.     local AP_RET=0
  197.  
  198.     if pidof $DAEMON > /dev/null 2>&1 ; then
  199.             if [ -e $PIDFILE ] && pidof $DAEMON | tr ' ' '\n' | grep -w $(cat $PIDFILE) > /dev/null 2>&1 ; then
  200.                     AP_RET=2
  201.             else
  202.                     AP_RET=1
  203.             fi
  204.     else
  205.         AP_RET=0
  206.     fi
  207.  
  208.     # AP_RET is:
  209.     # 0 if Apache (whichever) is not running
  210.     # 1 if Apache (whichever) is running
  211.     # 2 if Apache from the PIDFILE is running
  212.  
  213.     if [ $AP_RET = 0 ] ; then
  214.             return 1
  215.     fi
  216.  
  217.     if [ $AP_RET = 2 ] && apache_conftest ; then
  218.             $APACHE2CTL $STOP > /dev/null 2>&1
  219.             apache_wait_stop $?
  220.             return $?
  221.     else
  222.             if [ $AP_RET = 2 ]; then
  223.                     clear_error_msg
  224.                     APACHE2_INIT_MESSAGE="The apache2$DIR_SUFFIX configtest failed, so we are trying to kill it manually. This is almost certainly suboptimal, so please make sure your system is working as you'd expect now!"
  225.                     killproc -p $PIDFILE $DAEMON
  226.                     apache_wait_stop $?
  227.                     return $?
  228.             elif [ $AP_RET = 1 ] ; then
  229.                     APACHE2_INIT_MESSAGE="There are processes named 'apache2' running which do not match your pid file which are left untouched in the name of safety, Please review the situation by hand".
  230.                     return 2
  231.             fi
  232.     fi
  233.  
  234. }
  235.  
  236.  
  237. #
  238. # Function that sends a SIGHUP to the daemon/service
  239. #
  240. do_reload() {
  241.     if apache_conftest; then
  242.             if ! pidofproc -p $PIDFILE "$DAEMON" > /dev/null 2>&1 ; then
  243.                     APACHE2_INIT_MESSAGE="Apache2 is not running"
  244.                     return 2
  245.             fi
  246.             $APACHE2CTL graceful > /dev/null 2>&1
  247.             return $?
  248.     else
  249.             APACHE2_INIT_MESSAGE="The apache2$DIR_SUFFIX configtest failed. Not doing anything."
  250.             return 2
  251.     fi
  252. }
  253.  
  254.  
  255. check_htcacheclean() {
  256.     [ "$HTCACHECLEAN_MODE" = "daemon" ] || return 1
  257.     [ "$HTCACHECLEAN_RUN"  = "yes"    ] && return 0
  258.  
  259.     MODSDIR=$(. $APACHE_ENVVARS && echo $APACHE_MODS_ENABLED)
  260.             [ "$HTCACHECLEAN_RUN"  = "auto" \
  261.                     -a -e ${MODSDIR:-$APACHE_CONFDIR/mods-enabled}/cache_disk.load ] && \
  262.                     return 0
  263.     return 1
  264. }
  265.  
  266. start_htcacheclean() {
  267.        $HTCACHECLEAN $HTCACHECLEAN_OPTIONS -d$HTCACHECLEAN_DAEMON_INTERVAL \
  268.             -i -p$HTCACHECLEAN_PATH -l$HTCACHECLEAN_SIZE
  269. }
  270.  
  271. stop_htcacheclean() {
  272.     pkill -P 1 -f "htcacheclean.* -p$HTCACHECLEAN_PATH " 2> /dev/null || return 1
  273. }
  274.  
  275.  
  276. # Sanity checks. They need to occur after function declarations
  277. [ -x $DAEMON ] || exit 0
  278.  
  279. if [ ! -x $DAEMON ] ; then
  280.     echo "No apache-bin package installed"
  281.     exit 0
  282. fi
  283.  
  284. if [ -z "$PIDFILE" ] ; then
  285.     echo ERROR: APACHE_PID_FILE needs to be defined in $APACHE_ENVVARS >&2
  286.     exit 2
  287. fi
  288.  
  289. if check_htcacheclean ; then
  290.     if [ ! -d "$HTCACHECLEAN_PATH" ] ; then
  291.             echo "htcacheclean is configured, but directory $HTCACHECLEAN_PATH does not exist!" >&2
  292.             exit 2
  293.     fi
  294. fi
  295.  
  296.  
  297.  
  298. case "$1" in
  299.   start)
  300.     log_daemon_msg "Starting $DESC" "$NAME"
  301.     do_start
  302.     RET_STATUS=$?
  303.     case "$RET_STATUS" in
  304.         0|1)
  305.             log_success_msg
  306.             [ "$VERBOSE" != no ] && [ $RET_STATUS = 1 ] && log_warning_msg "Server was already running"
  307.             if check_htcacheclean ; then
  308.                 [ "$VERBOSE" != no ] && log_daemon_msg "Starting HTTP cache cleaning daemon" "htcacheclean"
  309.                 start_htcacheclean
  310.                 [ "$VERBOSE" != no ] && log_end_msg $?
  311.             fi
  312.             ;;
  313.         2)
  314.             log_failure_msg
  315.             print_error_msg
  316.             exit 1
  317.             ;;
  318.     esac
  319.     ;;
  320.   stop|graceful-stop)
  321.     log_daemon_msg "Stopping $DESC" "$NAME"
  322.     do_stop "$1"
  323.     RET_STATUS=$?
  324.     case "$RET_STATUS" in
  325.         0|1)
  326.             log_success_msg
  327.             [ "$VERBOSE" != no ] && [ $RET_STATUS = 1 ] && log_warning_msg "Server was not running"
  328.             ;;
  329.         2)
  330.             log_failure_msg
  331.             print_error_msg
  332.             exit 1
  333.             ;;
  334.     esac
  335.     print_error_msg
  336.  
  337.     if check_htcacheclean ; then
  338.         [ "$VERBOSE" != no ] && log_daemon_msg "Stopping HTTP cache cleaning daemon" "htcacheclean"
  339.         stop_htcacheclean
  340.         [ "$VERBOSE" != no ] && log_end_msg $?
  341.     fi
  342.  
  343.     ;;
  344.   status)
  345.     status_of_proc -p $PIDFILE "apache2" "$NAME"
  346.     exit $?
  347.     ;;
  348.   reload|force-reload|graceful)
  349.     log_daemon_msg "Reloading $DESC" "$NAME"
  350.     do_reload
  351.     RET_STATUS=$?
  352.     case "$RET_STATUS" in
  353.         0|1)
  354.             log_success_msg
  355.             [ "$VERBOSE" != no ] && [ $RET_STATUS = 1 ] && log_warning_msg "Server was already running"
  356.             ;;
  357.         2)
  358.             log_failure_msg
  359.             print_error_msg
  360.             exit 1
  361.             ;;
  362.     esac
  363.     print_error_msg
  364.     ;;
  365.   restart)
  366.     log_daemon_msg "Restarting $DESC" "$NAME"
  367.     do_stop stop
  368.     case "$?" in
  369.         0|1)
  370.             do_start
  371.             case "$?" in
  372.                 0)
  373.                     log_end_msg 0
  374.                     ;;
  375.                 1|*)
  376.                     log_end_msg 1 # Old process is still or failed to running
  377.                     print_error_msg
  378.                     exit 1
  379.                     ;;
  380.             esac
  381.             ;;
  382.         *)
  383.             # Failed to stop
  384.             log_end_msg 1
  385.             print_error_msg
  386.             exit 1
  387.             ;;
  388.     esac
  389.     ;;
  390.   start-htcacheclean)
  391.     log_daemon_msg "Starting htcacheclean"
  392.     start_htcacheclean
  393.     log_end_msg $?
  394.     exit $?
  395.     ;;
  396.   stop-htcacheclean)
  397.     log_daemon_msg "Stopping htcacheclean"
  398.     stop_htcacheclean
  399.     log_end_msg $?
  400.     exit $?
  401.     ;;
  402.   *)
  403.     echo "Usage: $SCRIPTNAME {start|stop|graceful-stop|restart|reload|force-reload|start-htcacheclean|stop-htcacheclean}" >&2
  404.     exit 3
  405.     ;;
  406. esac
  407.  
  408. exit 0
  409.  
  410. # vim: syntax=sh ts=4 sw=4 sts=4 sr noet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement