Koozali.org: home of the SME Server

Flush Squid cache from cgi script

Ian Wells

Flush Squid cache from cgi script
« on: October 30, 2002, 05:18:10 PM »
Today I wanted to flush the Squid cache, and after searching here I was able to do it from the command line. I then thought it shoud be easy to make this a CGI script.

The problem is that it works, but errors appear in httpd/error_log

/sbin/service: basename: No such file or directory
/sbin/service: basename: No such file or directory
squid: ERROR: No running copy

So my questions are, why do these errors appear, and should I being doing something different? my Methods 1,2 give the same results - I would be interested in comments on how system calls should be embedded in CGI scripts.

Method 1

 my $stopCache  = /sbin/service squid stop;
 my $flushCache  = echo "" > /var/spool/squid/swap.state;
 my $startCache  = /sbin/service squid start;

 if ($startCache !~ /OK/)
    die ("Error occurred while flushing WebCache.\n");

Method 2

 my @args = ("/sbin/service", "squid", "stop");
 system (@args) == 0
    or die ("Error occurred while flushing WebCache.\n");
    
 my $flushCache  = echo "" > /var/spool/squid/swap.state;

 @args = ("/sbin/service", "squid", "start");
 system (@args) == 0
    or die ("Error occurred while flushing WebCache.\n");

Charlie Brady

Re: Flush Squid cache from cgi script
« Reply #1 on: October 30, 2002, 06:38:26 PM »
Ian Wells wrote:
 
> Today I wanted to flush the Squid cache

Why?

> So my questions are, why do these errors appear

Probably because $PATH is not set correctly in the scripts.

Charlie

Ian Wells

Re: Flush Squid cache from cgi script
« Reply #2 on: October 30, 2002, 07:01:36 PM »
>> Today I wanted to flush the Squid cache

>Why?

The main reason was that my main WinXP machine with IE6 was showing the cached copy of the new pages on my externally hosted web-site. Looking at the squid logs it was serving up the pages from the cache - although I know that the pages were updated (and could see them with the FTP client).

After flushing the squid cache (and nothing else) all was well.

It also had the benefit of Google now displaying correctly, instead of strange flags.

This happened on 5.1.2 (I have not yet upgraded my main machine).

I know that I should have investigated further and tried it on 5.6, but I wanted to get my niece's web site up today:)

>Probably because $PATH is not set correctly in the scripts.

Maybe, but I give the full path name, and the path is cleared. This CGI script is based on one of the server manager actions. I admit that I know nearly nothing about CGI scripts.
    $ENV {'PATH'} = '';

This does not give an error
my $uptime = /usr/bin/uptime;

This does give an error
my $status = /sbin/service squid status;

Abe Loveless

Re: Flush Squid cache from cgi script
« Reply #3 on: December 05, 2002, 06:02:02 AM »
Ian,

Did you get your script working?  Do you have it posted somewhere?

Did you make it into a server-manager panel, or just create the cgi-script?

Thanks.

Ian Wells

Re: Flush Squid cache from cgi script
« Reply #4 on: December 05, 2002, 11:25:45 AM »
Abe,

The cgi-script works, although a few lines appear in httpd/error_log
"/sbin/service: basename: No such file or directory"

At the moment it is only tested on 5.1.2, but will be moved to 5.6 during this month.

This subroutine is the main part of the script. Would you like me to email you the current script?

sub performAndShowResult ($)
{

 my ($q) = @_;
 
 #-----------------------------------------------------------
 # Flush the Web-Cache.
 #------------------------------------------------------------
 my @args = ("/sbin/service", "squid", "stop");
 system (@args) == 0
    or die ("Error occurred while flushing WebCache.\n");
    
 my $flushCache  = echo "" > /var/spool/squid/swap.state;

 @args = ("/sbin/service", "squid", "start");
 system (@args) == 0
    or die ("Error occurred while flushing WebCache.\n");

 return;
}

Abe Loveless

Re: Flush Squid cache from cgi script
« Reply #5 on: December 05, 2002, 05:34:26 PM »
Thanks.  That's essentially what I'm doing, as well.


#------------------------------------------------------------
# subroutine to flush the squid cache
#------------------------------------------------------------

sub flushCache ($)
{
    system ("/etc/rc.d/init.d/squid stop") == 0
        or die ("Error occurred while stopping squid.\n");

    system ("echo \"\" > /var/spool/squid/swap.state") == 0
        or die ("Error occurred while flushing cache.\n");

    system ("/etc/rc.d/init.d/squid start") == 0
        or die ("Error occurred while starting squid.\n");

    showInitial ($q, "Squid Cache has been cleared successfully. [ refresh ]");
    return;
}


I've got it integrated into a server-manager panel.  I'm adding some other Squid Properties that seem useful, then I'll post it here.

Thanks, again.

Abe Loveless

squidProperties Panel
« Reply #6 on: December 06, 2002, 06:39:13 AM »
If you've got a spare server, take a look at this.

http://www.tech-geeks.org/contrib/loveless/beta/squidProperties/

Ian Wells

Re: squidProperties Panel
« Reply #7 on: December 06, 2002, 10:58:01 AM »
I've tested the 'Flush Squid WebCache.' on a 5.1.2 server, and it worked as expected.

Ian

Abe Loveless

Re: squidProperties Panel
« Reply #8 on: December 06, 2002, 11:53:17 PM »
Great.  So that takes care of 5.1.2, and I heard from someone that 5.0 was successful as well.

Sounds like it's working as expected.  :)

Thanks for the feedback.

steve lewis

Re: squidProperties Panel
« Reply #9 on: December 07, 2002, 07:25:29 AM »
Abe,

I have installed your squidProperties Panel on my 5.5 box and works great.

Needed it to resolve an issue for a web site my wife is involved with. Her updates to the page was not visible to her, yet others outside our network could see them.

Thanks for your panel.

Steve Lewis
Fayetteville, AR , USA

Teddy Wang

More importantly how do you turn off squids caching feature.
« Reply #10 on: December 09, 2002, 12:23:09 PM »
I've found that squid seems to be caching static .html files indefinitely.  This makes remote web development difficult.  How do I turn it off?

Thanks!

steve lewis

Re: squidProperties Panel
« Reply #11 on: December 09, 2002, 06:19:42 PM »
Teddy,

About 4 posts up, Abe Loveless has a squidProperties Panel that can be found at the tech-geeks link below.

http://www.tech-geeks.org/contrib/loveless/beta/squidProperties/

I recently installed this in my 5.5 box because the web site my wife is involved with was not showing her edits due to our cache.

This is just what you need.

Steve Lewis

Abe Loveless

Re: squidProperties Panel BUG
« Reply #12 on: December 16, 2002, 06:15:19 PM »
Just found a bug in the squidProperties panel.  A squid restart was returning an error, which may have been showing up in your squidguard/dansguardian update:

Here is the fix:

1. Login as Root
2. Change to templates-custom directory
   - "cd /etc/e-smith/templates-custom/etc/squid/squid.conf"
3. Edit file
   - "pico -w 80always_direct60no-cache"
4. On line 27, make the change:
   Current:   $result .= "always direct allow all_direct \n";
   Should be:   $result .= "always_direct allow all_direct \n";

You have to take out the space and add an underscore "_" between "always direct".

5. Expand template:
   - "/sbin/e-smith/expand-template /etc/squid/squid.conf"
6. Restart squid:
   - "service squid restart"
7. Done.

I'll update the tarball this morning.

Abe Loveless

Re: squidProperties Panel BUG
« Reply #13 on: December 18, 2002, 03:59:49 AM »
I believe that the 2nd piece of this application isn't working on servers less than 5.5.  The squid flush does work, but the direct domain piece seems to cause some Perl errors due to some changes in 5.5.

I'll try to update the code that is causing the problem in the next few days.