Re: [squid-users] FYI: squidctl script

From: Chris McDonough <chrism@dont-contact.us>
Date: Thu, 10 Jan 2002 21:32:13 -0500

Henrik Nordstrom wrote:
> This should perhaps only run squid -z once... not once per cache_dir..

Oops, yes. Attached is a new version which fixes that bug plus another
one that got the cache.log directive name wrong.

-- 
Chris McDonough                    Zope Corporation
http://www.zope.org             http://www.zope.com
"Killing hundreds of birds with thousands of stones"

#!/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_log $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
  if [ ! -d $CACHE_SWAP/00 ]; then
      echo -n "Initializing squid cache_dir $CACHE_SWAP... "
      $SQUID -z -F 2>/dev/null
  fi
    
  # 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 Thu Jan 10 2002 - 19:25:13 MST

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