Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.92 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: Start/stop apache2 web server
  10. ### END INIT INFO
  11.  
  12. set -e
  13.  
  14. SCRIPTNAME="${0##*/}"
  15. SCRIPTNAME="${SCRIPTNAME##[KS][0-9][0-9]}"
  16. if [ -n "$APACHE_CONFDIR" ] ; then
  17.     if [ "${APACHE_CONFDIR##/etc/apache2-}" != "${APACHE_CONFDIR}" ] ; then
  18.         DIR_SUFFIX="${APACHE_CONFDIR##/etc/apache2-}"
  19.     else
  20.         DIR_SUFFIX=
  21.     fi
  22. elif [ "${SCRIPTNAME##apache2-}" != "$SCRIPTNAME" ] ; then
  23.     DIR_SUFFIX="-${SCRIPTNAME##apache2-}"
  24.     APACHE_CONFDIR=/etc/apache2$DIR_SUFFIX
  25. else
  26.     DIR_SUFFIX=
  27.     APACHE_CONFDIR=/etc/apache2
  28. fi
  29. if [ -z "$APACHE_ENVVARS" ] ; then
  30.     APACHE_ENVVARS=$APACHE_CONFDIR/envvars
  31. fi
  32. export APACHE_CONFDIR APACHE_ENVVARS
  33.  
  34. ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin"
  35. if [ "$APACHE_CONFDIR" != /etc/apache2 ] ; then
  36.     ENV="$ENV APACHE_CONFDIR=$APACHE_CONFDIR"
  37. fi
  38. if [ "$APACHE_ENVVARS" != "$APACHE_CONFDIR/envvars" ] ; then
  39.     ENV="$ENV APACHE_ENVVARS=$APACHE_ENVVARS"
  40. fi
  41.  
  42.  
  43. #edit /etc/default/apache2 to change this.
  44. HTCACHECLEAN_RUN=auto
  45. HTCACHECLEAN_MODE=daemon
  46. HTCACHECLEAN_SIZE=300M
  47. HTCACHECLEAN_DAEMON_INTERVAL=120
  48. HTCACHECLEAN_PATH=/var/cache/apache2$DIR_SUFFIX/mod_disk_cache
  49. HTCACHECLEAN_OPTIONS=""
  50.  
  51. APACHE_HTTPD=$(. $APACHE_ENVVARS && echo $APACHE_HTTPD)
  52. if [ -z "$APACHE_HTTPD" ] ; then
  53.     APACHE_HTTPD=/usr/sbin/apache2
  54. fi
  55. if [ ! -x $APACHE_HTTPD ] ; then
  56.     echo "No apache MPM package installed"
  57.     exit 0
  58. fi
  59.  
  60. . /lib/lsb/init-functions
  61.  
  62. test -f /etc/default/rcS && . /etc/default/rcS
  63.  
  64. if [ -f /etc/default/apache2$DIR_SUFFIX ] ; then
  65.     . /etc/default/apache2$DIR_SUFFIX
  66. elif [ -f /etc/default/apache2 ] ; then
  67.     . /etc/default/apache2
  68. fi
  69.  
  70. APACHE2CTL="$ENV /usr/sbin/apache2ctl"
  71. HTCACHECLEAN="$ENV /usr/sbin/htcacheclean"
  72.  
  73. PIDFILE=$(. $APACHE_ENVVARS && echo $APACHE_PID_FILE)
  74. if [ -z "$PIDFILE" ] ; then
  75.     echo ERROR: APACHE_PID_FILE needs to be defined in $APACHE_ENVVARS >&2
  76.     exit 2
  77. fi
  78.  
  79.  
  80. check_htcacheclean() {
  81.     [ "$HTCACHECLEAN_MODE" = "daemon" ] || return 1
  82.  
  83.     [ "$HTCACHECLEAN_RUN"  = "yes"    ] && return 0
  84.  
  85.     MODSDIR=$(. $APACHE_ENVVARS && echo $APACHE_MODS_ENABLED)
  86.     [ "$HTCACHECLEAN_RUN"  = "auto" \
  87.       -a -e ${MODSDIR:-$APACHE_CONFDIR/mods-enabled}/disk_cache.load ] && \
  88.         return 0
  89.    
  90.     return 1
  91. }
  92.  
  93. start_htcacheclean() {
  94.     if [ ! -d "$HTCACHECLEAN_PATH" ] ; then
  95.         echo "... directory $HTCACHECLEAN_PATH does not exist!" >&2
  96.         return 1
  97.     fi 
  98.     $HTCACHECLEAN $HTCACHECLEAN_OPTIONS -d$HTCACHECLEAN_DAEMON_INTERVAL \
  99.             -i -p$HTCACHECLEAN_PATH -l$HTCACHECLEAN_SIZE
  100. }
  101.  
  102. stop_htcacheclean() {
  103.     pkill -P 1 -f "htcacheclean.* -p$HTCACHECLEAN_PATH " 2> /dev/null || echo ...not running
  104. }
  105.  
  106. pidof_apache() {
  107.     # if there is actually an apache2 process whose pid is in PIDFILE,
  108.     # print it and return 0.
  109.     if [ -e "$PIDFILE" ]; then
  110.         if pidof apache2 | tr ' ' '\n' | grep -w $(cat $PIDFILE); then
  111.             return 0
  112.         fi
  113.     fi
  114.     return 1
  115. }
  116.  
  117. apache_stop() {
  118.     if $APACHE2CTL configtest > /dev/null 2>&1; then
  119.         # if the config is ok than we just stop normaly
  120.                 $APACHE2CTL stop 2>&1 | grep -v 'not running' >&2 || true
  121.     else
  122.         # if we are here something is broken and we need to try
  123.         # to exit as nice and clean as possible
  124.         PID=$(pidof_apache) || true
  125.  
  126.         if [ "${PID}" ]; then
  127.             # in this case it is everything nice and dandy and we kill apache2
  128.             echo
  129.             log_warning_msg "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!"
  130.                         kill $PID
  131.         elif [ "$(pidof apache2)" ]; then
  132.             if [ "$VERBOSE" != no ]; then
  133.                                 echo " ... failed!"
  134.                     echo "You may still have some apache2 processes running.  There are"
  135.                     echo "processes named 'apache2' which do not match your pid file,"
  136.                     echo "and in the name of safety, we've left them alone.  Please review"
  137.                     echo "the situation by hand."
  138.                         fi
  139.                         return 1
  140.         fi
  141.     fi
  142. }
  143.  
  144. apache_wait_stop() {
  145.     # running ?
  146.     PIDTMP=$(pidof_apache) || true
  147.     if kill -0 "${PIDTMP:-}" 2> /dev/null; then
  148.         PID=$PIDTMP
  149.     fi
  150.  
  151.     apache_stop
  152.  
  153.     # wait until really stopped
  154.     if [ -n "${PID:-}" ]; then
  155.         i=0
  156.         while kill -0 "${PID:-}" 2> /dev/null;  do
  157.                 if [ $i = '60' ]; then
  158.                     break;
  159.                 else
  160.                     if [ $i = '0' ]; then
  161.                             echo -n " ... waiting "
  162.                     else
  163.                                 echo -n "."
  164.                     fi
  165.                     i=$(($i+1))
  166.                     sleep 1
  167.                   fi
  168.          done
  169.     fi
  170. }
  171.  
  172. case $1 in
  173.     start)
  174.         log_daemon_msg "Starting web server" "apache2"
  175.         if $APACHE2CTL start; then
  176.             if check_htcacheclean ; then
  177.                 log_progress_msg htcacheclean
  178.                 start_htcacheclean || log_end_msg 1
  179.             fi
  180.                         log_end_msg 0
  181.                 else
  182.                         log_end_msg 1
  183.                 fi
  184.     ;;
  185.     stop)
  186.         if check_htcacheclean ; then
  187.             log_daemon_msg "Stopping web server" "htcacheclean"
  188.             stop_htcacheclean
  189.             log_progress_msg "apache2"
  190.         else
  191.             log_daemon_msg "Stopping web server" "apache2"
  192.         fi
  193.         if apache_wait_stop; then
  194.                         log_end_msg 0
  195.                 else
  196.                         log_end_msg 1
  197.                 fi
  198.     ;;
  199.     graceful-stop)
  200.         if check_htcacheclean ; then
  201.             log_daemon_msg "Stopping web server" "htcacheclean"
  202.             stop_htcacheclean
  203.             log_progress_msg "apache2"
  204.         else
  205.             log_daemon_msg "Stopping web server" "apache2"
  206.         fi
  207.         if $APACHE2CTL graceful-stop; then
  208.                         log_end_msg 0
  209.                 else
  210.                         log_end_msg 1
  211.                 fi
  212.     ;;
  213.     reload | force-reload | graceful)
  214.                 log_daemon_msg "Reloading web server config"
  215.                 if pidof_apache > /dev/null ; then
  216.                     log_progress_msg "apache2"
  217.                     if ! $APACHE2CTL configtest > /dev/null 2>&1; then
  218.                         log_end_msg 1
  219.                         $APACHE2CTL configtest || true
  220.                         exit 1
  221.                     fi
  222.                     if $APACHE2CTL graceful $2 ; then
  223.                         log_end_msg 0
  224.                     else
  225.                         log_end_msg 1
  226.                     fi
  227.                 else
  228.                     log_progress_msg "apache2 not running"
  229.                     log_end_msg 0
  230.                 fi
  231.     ;;
  232.     restart)
  233.         if ! $APACHE2CTL configtest > /dev/null 2>&1; then
  234.             $APACHE2CTL configtest || true
  235.             log_end_msg 1
  236.             exit 1
  237.         fi
  238.         if check_htcacheclean ; then
  239.             log_daemon_msg "Restarting web server" "htcacheclean"
  240.             stop_htcacheclean
  241.             log_progress_msg apache2
  242.         else
  243.             log_daemon_msg "Restarting web server" "apache2"
  244.         fi
  245.         PID=$(pidof_apache) || true
  246.         if ! apache_wait_stop; then
  247.                         log_end_msg 1 || true
  248.                 fi
  249.         if $APACHE2CTL start; then
  250.             if check_htcacheclean ; then
  251.                 start_htcacheclean || log_end_msg 1
  252.             fi
  253.                         log_end_msg 0
  254.                 else
  255.                         log_end_msg 1
  256.                 fi
  257.     ;;
  258.     start-htcacheclean)
  259.         log_daemon_msg "Starting htcacheclean"
  260.         start_htcacheclean || log_end_msg 1
  261.         log_end_msg 0
  262.     ;;
  263.     stop-htcacheclean)
  264.         log_daemon_msg "Stopping htcacheclean"
  265.             stop_htcacheclean
  266.             log_end_msg 0
  267.     ;;
  268.     status)
  269.         PID=$(pidof_apache) || true
  270.         if [ -n "$PID" ]; then
  271.             echo "Apache2$DIR_SUFFIX is running (pid $PID)."
  272.             exit 0
  273.         else
  274.             echo "Apache2$DIR_SUFFIX is NOT running."
  275.             if [ -e "$PIDFILE" ]; then
  276.                 exit 1
  277.             else
  278.                 exit 3
  279.             fi
  280.         fi
  281.     ;;
  282.     *)
  283.         log_success_msg "Usage: /etc/init.d/apache2$DIR_SUFFIX {start|stop|graceful-stop|restart|reload|force-reload|start-htcacheclean|stop-htcacheclean|status}"
  284.         exit 1
  285.     ;;
  286. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement