This versatile script tests for increases in a file's size. I wrote it as a 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.
cd /usr/local/apache/logs /usr/bin/stat -s access_log | cut -d" " -f8 | cut -c9-20 > new_size if [ $(more new_size) -gt $(more prev_size) ]; then /sc/apache/emailapacheused.sh else echo fi rm prev_size mv new_size prev_size
Note always use full paths in scripts or they won't work properly. The second 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 new_size
On the third line, the script compares a newly-created new_size file to the previous prev_size file, to see if it is bigger.
If the newer file is larger in size, then line four sends an administrative alert email using a second script, emailapacheused.sh
If the file size hasn't changed then it simply echoes nothing.
The older prev_size file is deleted and finally the script renames (moves) the new_size file to prev_size and is ready to run again.
Note that before the script runs for the first time, be sure to create an original prev_size file for new_size to be compared to. This can be achieved, by running the second line of the script manually and outputting it to /usr/local/apache/logs/prev_size.
The code can easily be modified to monitor anything that a command like stat can tell you about a file.
AndyM | Updated July 2019