Scheduled Crontab Job with Shell Script that Clears Cache and sends an Email

By James Myers


  • 17 September 2014
  • Crontab, Email, Bash, Script, Shell

This is a 3 part task/tutorial that you can take apart and use for your own devices, this fundamentally breaks down into:

 

Background Story

In order to improve the performance of your website you may implement server side caching of your dynamically generated content such as rendered blocks of HTML, database results sets or data/content that has come from a 3rd party website or API.

Now whilst caching content is a good idea it is also a good idea to do a bit of housekeeping every now and then, such as removing old cache file that are consuming space and resources. If say for instance the maximum cache age of file is a day then there is little point in keeping cache files that are older than a day.

But why does it matter?

Well on a small site that receives a small amount of traffic you may not notice the Cache directory fill up too fast, but on a larger site which receives a lot of traffic this Cache directory can soon become very large and if it is never cleared then when your web application goes to look for the cache file it may take longer than returning data from the original source if there are too many files to search for. There are other reasons why clearing the cache could be useful such as clearing space and clearing badly formatted cache files.

 

Now by any means this is not the only solution or the best for improving performance or even the optimum caching solution; implementing a caching server/proxy for instance could be a better option, inceasing HD size/installing more drives etc but this is outside of the scope of this article and to serve our immediate purposes it ticked several boxes:

 

  • The website cached content for no longer than a day.
  • The servers received a fair amount of traffic so cache was building up relatively fast.
  • Server Space was an issue if cache was left to continually build.
  • Time and resources was also an issue.

 

So we decided upon this solution:

 

Creating a Scheduled Job on a Server using Crontab

 

If you are not familar with Cron Jobs there is an excellent article on Tuts which I recommend reading first:  Cron Jobs Article

 

Essentially we open up the crontab editor using the command below:

 

crontab -e -u apache

 

The -e flag is for edit and the -u flag followed by the username is to run the job as a particular user (we use the user apache as we need an account that has permission to run the script and remove the cache files, be careful if using the user root).  The crontab will then open up in your default editor. Now if the script that we want to run is located in /var/www/ and called clearCache.sh and we want to run the script at 1 minute past 5 every morning (this was good time for me as was websites least busiest time) then we would create a job by entering the text below:

 

1 5 * * * sh /var/www/clearCache.sh

The syntax in entry above breaks down like so

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- day of week (0 - 6) (Sunday=0)
| | | ------- month (1 - 12)
| | --------- day of month (1 - 31)
| ----------- hour (0 - 23)
------------- min (0 - 59)

 

Once happy with the crontab if you save and exit if succesful you should see a message that says something like 'Installing New Crontab'.  Next we need to create the actual script.

 

Creating a Shell Script that executes a job such as clearing cache

To create the script mentoned in the Crontab entry above, first we change to directory we want to store the file using the below command:

 

cd /var/www/

Then to create and open the file 'clearCache.sh' using my preferred text editor VI or VIM you would type (if you are not sure how to use VI other editors such as Nano may be easier for you to use) :

vi clearCache.sh

Now in the editor of your choice we will add the following code. The first line of code #!/bin/bash is known as a 'Hash Bang' or 'She bang' you put at the start of your script and tells Linux how to interpret the file. The remainder of the code I will breakdown in the comments within the code:

Sending an email to alert you when a Scheduled task has run

#!/bin/bash

# Create a variable called EMAILMESSAGE and give it 
# the value of a temporary filename you can use to 
# put the contents of your email message
EMAILMESSAGE="/tmp/emailmessage.txt"

# Create the subject line for your email message by printing
# out the string and the > redirects the output to the first
# line of the temporary file given in the EMAILMESSAGE variable
echo "Subject: Cron Job - Clear Server Cache"> $EMAILMESSAGE

# We append another line to the EMAILMESSAGE file using the ;>> operator
echo "Space before clear cache">> $EMAILMESSAGE

# So that we can know the amount of space before 
# the clear we use the df -h command and append
# the output to our EMAILSMESSAGE file
df -h >> $EMAILMESSAGE

# this message will not appear in our EMAILSMESSAGE file but 
# if we were to run the script manually we would see the 
# message printed to the screen
echo "removing files in _Cache/*"

# This line removes all of our cache files
# located in the directory /var/www/example.com/_Cache/ 
# you will need to change this to wherever your cache files are located
# You should also be aware of the rm command and its flags:
#  - the -R flag says remove recursively, so any file in
# any sub directory of our cache directory
#  - the -f flag says force remove, so we won't receive any prompts
rm -fR /var/www/example.com/_Cache/*

# We create another variable EMAIL to store the 
# email address we want to send the alert to

EMAIL="[email protected]"

# append another line of text to our EMAILMESSAGE file
echo "Space after clear cache">> $EMAILMESSAGE

# again we use the df -h command, this time it is to print 
# the amount of space after clearing our cache
# this can be a good indication to know that the actual
# script is working and deleting our files
df -h >> $EMAILMESSAGE

# append a final line of text to our EMAILMESSAGE
echo "Example.com Cache cleared on Server">> $EMAILMESSAGE

# send an email using sendmail program, to the email address
# stored in our EMAIL variable
# with the content stored in our temporary EMAILMESSAGE file

sendmail "$EMAIL" < $EMAILMESSAGE

Once you have finished save and exit the script.  One final thing you should do is you should use chmod command to ensure that the script is executable.  To do this enter the command below

chmod +x clearCache.sh