Advertisement
Guest User

Squid init script

a guest
Jun 24th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.91 KB | None | 0 0
  1. #!/bin/bash
  2. # chkconfig: - 90 25
  3. # pidfile: /var/run/squid/squid.pid
  4. # config: /etc/squid/squid.conf
  5. #
  6. ### BEGIN INIT INFO
  7. # Provides: squid
  8. # Short-Description: starting and stopping Squid Internet Object Cache
  9. # Description: Squid - Internet Object Cache. Internet object caching is \
  10. #       a way to store requested Internet objects (i.e., data available \
  11. #       via the HTTP, FTP, and gopher protocols) on a system closer to the \
  12. #       requesting site than to the source. Web browsers can then use the \
  13. #       local Squid cache as a proxy HTTP server, reducing access time as \
  14. #       well as bandwidth consumption.
  15. ### END INIT INFO
  16.  
  17.  
  18. PATH=/usr/bin:/sbin:/bin:/usr/sbin
  19. export PATH
  20.  
  21. # Source function library.
  22. . /etc/rc.d/init.d/functions
  23.  
  24. # Source networking configuration.
  25. . /etc/sysconfig/network
  26.  
  27. if [ -f /etc/sysconfig/squid ]; then
  28.     . /etc/sysconfig/squid
  29. fi
  30.  
  31. # don't raise an error if the config file is incomplete
  32. # set defaults instead:
  33. SQUID_OPTS=${SQUID_OPTS:-""}
  34. SQUID_PIDFILE_TIMEOUT=${SQUID_PIDFILE_TIMEOUT:-30}
  35. SQUID_SHUTDOWN_TIMEOUT=${SQUID_SHUTDOWN_TIMEOUT:-100}
  36. SQUID_CONF=${SQUID_CONF:-"/etc/squid/squid.conf"}
  37. SQUID_PIDDIR="/var/run/squid"
  38. SQUID_PIDFILE="$SQUID_PIDDIR/squid.pid"
  39. SQUID_USER="squid"
  40. SQUID_GROUP="squid"
  41.  
  42. # determine the name of the squid binary
  43. [ -f /usr/sbin/squid ] && SQUID=squid
  44.  
  45. prog="$SQUID"
  46.  
  47. # determine which one is the cache_swap directory
  48. CACHE_SWAP=`sed -e 's/#.*//g' $SQUID_CONF | \
  49.     grep cache_dir | awk '{ print $3 }'`
  50.  
  51. RETVAL=0
  52.  
  53. probe() {
  54.     # Check that networking is up.
  55.     [ ${NETWORKING} = "no" ] && exit 1
  56.  
  57.     [ `id -u` -ne 0 ] && exit 4
  58.  
  59.     # check if the squid conf file is present
  60.     [ -f $SQUID_CONF ] || exit 6
  61. }
  62.  
  63. # create squid dir if it not exists
  64. if [ ! -d $SQUID_PIDDIR ] ; then mkdir $SQUID_PIDDIR ; chown -R $SQUID_USER.$SQUID_GROUP $SQUID_PIDDIR; fi
  65.  
  66. start() {
  67.     probe
  68.     parse=`$SQUID -k parse -f $SQUID_CONF 2>&1`
  69.     RETVAL=$?
  70.     if [ $RETVAL -ne 0 ]; then
  71.         echo -n $"Starting $prog: "
  72.         echo_failure
  73.         echo
  74.         echo "$parse"
  75.         return 1
  76.     fi
  77.     for adir in $CACHE_SWAP; do
  78.         if [ ! -d $adir/00 ]; then
  79.             echo -n "init_cache_dir $adir... "
  80.             $SQUID -z -F -f $SQUID_CONF >> /var/log/squid/squid.out 2>&1
  81.         fi
  82.     done
  83.     echo -n $"Starting $prog: "
  84.     $SQUID $SQUID_OPTS -f $SQUID_CONF >> /var/log/squid/squid.out 2>&1
  85.     RETVAL=$?
  86.     if [ $RETVAL -eq 0 ]; then
  87.         timeout=0;
  88.         while : ; do
  89.             [ ! -f $SQUID_PIDFILE ] || break
  90.             if [ $timeout -ge $SQUID_PIDFILE_TIMEOUT ]; then
  91.                 RETVAL=1
  92.                 break
  93.             fi
  94.             sleep 2 && echo -n "."
  95.             timeout=$((timeout+1))
  96.         done
  97.     fi
  98.     [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SQUID
  99.     [ $RETVAL -eq 0 ] && echo_success
  100.     [ $RETVAL -ne 0 ] && echo_failure
  101.     echo
  102.     return $RETVAL
  103. }
  104.  
  105. stop() {
  106.     echo -n $"Stopping $prog: "
  107.     $SQUID -k check -f $SQUID_CONF >> /var/log/squid/squid.out 2>&1
  108.     RETVAL=$?
  109.     if [ $RETVAL -eq 0 ] ; then
  110.         $SQUID -k shutdown -f $SQUID_CONF &
  111.         rm -f /var/lock/subsys/$SQUID
  112.         timeout=0
  113.         while : ; do
  114.             [ -f /var/run/squid.pid ] || break
  115.             if [ $timeout -ge $SQUID_SHUTDOWN_TIMEOUT ]; then
  116.                 echo
  117.                 return 1
  118.             fi
  119.             sleep 2 && echo -n "."
  120.             timeout=$((timeout+2))
  121.         done
  122.         echo_success
  123.         echo
  124.     else
  125.         echo_failure
  126.         if [ ! -e /var/lock/subsys/$SQUID ]; then
  127.             RETVAL=0
  128.         fi
  129.         echo
  130.     fi
  131.     sleep 1
  132.     killall -u $SQUID_USER
  133.     rm -rf $SQUID_PIDDIR/*
  134.     return $RETVAL
  135. }
  136.  
  137. reload() {
  138.     $SQUID $SQUID_OPTS -k reconfigure -f $SQUID_CONF
  139. }
  140.  
  141. restart() {
  142.     stop
  143.     sleep 1
  144.     start
  145. }
  146.  
  147. condrestart() {
  148.     [ -e /var/lock/subsys/squid ] && restart || :
  149. }
  150.  
  151. rhstatus() {
  152.     status $SQUID && $SQUID -k check -f $SQUID_CONF
  153. }
  154.  
  155.  
  156. case "$1" in
  157. start)
  158.     start
  159.     ;;
  160.  
  161. stop)
  162.     stop
  163.     ;;
  164.  
  165. reload|force-reload)
  166.     reload
  167.     ;;
  168.  
  169. restart)
  170.     restart
  171.     ;;
  172.  
  173. condrestart|try-restart)
  174.     condrestart
  175.     ;;
  176.  
  177. status)
  178.     rhstatus
  179.     ;;
  180.  
  181. probe)
  182.     probe
  183.     ;;
  184.  
  185. *)
  186.     echo $"Usage: $0 {start|stop|status|reload|force-reload|restart|try-restart|probe}"
  187.     exit 2
  188. esac
  189.  
  190. exit $?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement