Shell Script To Clean Lighttpd Web Server Cache

This shell script will clean lighttpd web server cache. You need to run this script via a cron job. Download install script at /etc/cron.daily directory.

#!/bin/bash
# Shell script to clean web server cache stored at /var/www/cache/ directory. 
# -------------------------------------------------------------------------
# Copyright (c) 2007 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
# Cache dir path
CROOT="/var/www/cachelighttpd/"
 
#Deleting files older than 10 days
DAYS=10
 
# Lighttpd user and group
LUSER="lighttpd"
LGROUP="lighttpd"
 
# start cleaning 
find ${CROOT} -type f -mtime +${DAYS} | xargs -r /bin/rm
 
# if directory missing just recreate it
if [ ! -d $CROOT ]
then 
	mkdir -p $CROOT
	chown ${LUSER}:${LGROUP} ${CROOT}
fi
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
Category List of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Dean Hall Dec 28, 2009 @ 12:10

    I use a similar script to clean my lighttpd cache, but I use two find commands with slightly different options:

    find ${CROOT} -type f -mtime +${DAYS} -delete
    find ${CROOT} -depth -mindepth 1 -type d -empty -delete

    The first find command deletes files older than $DAYS, just like yours. I use the “-delete” option so I don’t fork an additional process to do deletion. If the list of files find wants to output is too large for bash, bash may bail out before executing xargs. Another option is to use “-exec rm -f ‘{}’ \;” to delete each file, but that forks a process for each file.

    The second find command deletes empty directories. It uses “-depth” to explicitly tell find to process directory contents first (so a directory whose contents are empty directories gets deleted also). (The “-delete” option implies “-depth”, but I like to be explicit.) The “-mindepth 1” option tells find not to do anything to (i.e., delete) ${CROOT} itself.

    I also use /bin/sh to improve performance on all my scripts (minimal, I know), and I don’t create the cache directory since it’s really a side-effect.

    My script in total:

    #!/bin/sh
    #
    PATH='/bin:/sbin:/usr/bin:/usr/sbin'

    dir='/var/lib/lighttpd/cache/compress'
    age='14'

    # Bail out if $dir doesn't exist.
    if test ! -d "$dir" ; then
    echo "'${dir}' does not exist." 1>&2
    exit 111
    fi

    # Delete all plain files older than $age days:
    find "$dir" -type f -mtime +${age} -delete
    # Delete all empty directories except $dir itself:
    find “$dir” -depth -mindepth 1 -type d -empty -delete

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.