[squid-users] FYI: squidctl script

From: Chris McDonough <[email protected]>
Date: Wed, 09 Jan 2002 14:20:25 -0500

I just spent a bit of time on this script for managing squid and thought
it might be of some help to other folks. As opposed to Red Hat's squid
rc script, it doesnt make (many) assumptions. Enjoy!

#!/bin/sh
##############################################################################
#
# Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################

# squidctl -- a script to manage a squid server suitable in rc scripts
# by Chris McDonough (chrism@zope.com)

# see if we're symlinked
if [ ! -z "`stat $0|grep 'Symbolic Link'`" ]; then
   if [ -z "`which realpath`" ]; then
     # must have the realpath command in PATH if we're symlinked
     # search Google for a suitable realpath command if you dont have one
     # (Linux usually doesnt)
     # realpath converts symlinks to their real path names
     echo "Need realpath command when $0 is a symbolic link!"
     exit 1
   else
     REALPATH="`realpath $0`"
   fi
else
   REALPATH="$0"
fi

reldir=`dirname $REALPATH`

# assumes squid.conf is in squid-relative etc directory
BINPREFIX=`cd $reldir/..; pwd`

# check if the squid conf file is present
[ -f $BINPREFIX/etc/squid.conf ] || exit 1

# determine the name of the squid binary
[ -f $BINPREFIX/bin/squid ] && SQUID=$BINPREFIX/bin/squid

# find the cache_dir
confline=`grep ^cache_dir $BINPREFIX/etc/squid.conf`
#echo "confline is $confline"
if [ -n "$confline" ]
   then
      CACHE_SWAP=`echo $confline|cut --delimiter=" " --fields=3`;
   else
      CACHE_SWAP=$BINPREFIX/cache;
fi

# find the pid filename
confline=`grep ^pid_filename $BINPREFIX/etc/squid.conf`;
if [ -n "$confline" ]
   then
      PID_FILENAME=`echo $confline|cut --delimiter=" " --fields=2`;
   else
      PID_FILENAME=$BINPREFIX/logs/squid.pid;
fi

# find the cache.log filename
confline=`grep ^cache_filename $BINPREFIX/etc/squid.conf`;
if [ -n "$confline" ]
   then
      CACHELOG_FILENAME=`echo $confline|cut --delimiter=" " --fields=2`;
   else
      CACHELOG_FILENAME=$BINPREFIX/logs/cache.log;
fi

SQUID_OPTS=""
RETVAL=0

case "$1" in

start)
   # make cache_dir if it doesn't exist
   for adir in $CACHE_SWAP; do
     if [ ! -d $adir/00 ]; then
       echo -n "Initializing cache_dir $adir... "
       $SQUID -z -F 2>/dev/null
     fi
   done

   # start squid
   WESTARTED=0
   $SQUID $SQUID_OPTS &
   RETVAL=$?
   if [ $RETVAL -eq 0 ]; then
     for dummy in 0 1 2 3 4 5 6 7 8 9; do
       if [ -f $PID_FILENAME ]; then
         break
       else
         echo -n "."; sleep 2
       fi
     done
   else
    echo "Start failed: error code $RETVAL"
    break
   fi

   if [ -f $PID_FILENAME ]; then
    echo "Started"
   else
    echo "Start failed: check $CACHELOG_FILENAME"
   fi

   ;;

stop)
   if [ ! -f $PID_FILENAME ]; then
     echo "Could not stop (no pidfile $PID_FILENAME, no running copy?)"
     exit 1
   fi
   $SQUID -k shutdown &
   RETVAL=$?
   if [ $RETVAL -eq 0 ] ; then
     for dummy in 0 1 2 3 4 5 6 7 8 9; do
       if [ -f $PID_FILENAME ]; then
         echo -n "."; sleep 2
       else
         break
       fi
     done
   else
     echo "Error stopping: $RETVAL"
   fi

   if [ ! -f $PID_FILENAME ]; then
    echo "Stopped"
   else
    echo "Stop failed: check $CACHELOG_FILENAME"
   fi
   ;;

reload)
   $SQUID $SQUID_OPTS -k reconfigure
   exit $?
   ;;

rotate)
   $SQUID $SQUID_OPTS -k rotate
   exit $?
   ;;

restart)
   $0 stop
   $0 start
   ;;

*)
echo "Usage: $0 {start|stop|reload|restart|rotate}"
exit 1
esac

exit $RETVAL
Received on Wed Jan 09 2002 - 12:03:26 MST

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 17:05:48 MST