This versatile script tests for increases in a file's size. I wrote it as a simple way to monitor an Apache web server's access log, which grows when a web page is viewed. It can be used to monitor any log file which increases in size when its application is accessed.
The script runs every five minutes as a cron job (hence full paths to all files are used) and utilises the wonderfully simple sendEmail to send an administrative alert..
The first line uses the stat command and cuts out all characters except the file size of the apache access_log file and then outputs this to a new file called access_size1
Note that before the script runs for the first time, I have already created an original access_size file for access_size1 to be compared to, by running the first line of the script manually and outputting it to /usr/local/apache/logs/access_size.
On the second line, the script compares a newly-created access_size1 file to the previous access_size file, to see if it is bigger.
If the newer file is larger in size, then line 3 sends an administrative alert email using a second script, emailapacheused.sh..
..whereas if the file size hasn't changed then it simply echos nothing.
The older access_size file is deleted and finally the script renames (moves) the access_size1 file to access_size and is ready to run again.
The code can easily be modified to monitor reductions in a file size or really, anything that a command like stat can tell you about a file.