CMD59864 improved log file cleanup
authorBill Erickson <berickxx@gmail.com>
Tue, 27 Jan 2015 18:37:05 +0000 (13:37 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
 * use -mtime instead of -ctime for finding files to delete
 * clean up empty directories
 * extend notes on why we're not using logrotate

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/misc-scripts/manage-log-files.sh

index f00e846..93da78d 100755 (executable)
@@ -5,6 +5,22 @@ set -eu
 # To future generations:  this script was a lot simpler 
 # to implement and test than a logrotate setup.
 
+# Specifically, the hurdle is that logratate is designed
+# to handle multiple log files in the last path component
+# (e.g. /my/path/to/logfiles/*). KCLS log directory tree
+# has several levels which are year, month and day. To
+# fully express what this script does a lot of hardcoded
+# configuration would need to be written.
+# While it still technically should be possible to
+# configure logrotate to do what this script achieves
+# it will be really cumbersome and significantly time
+# demanding. Especially, when the log directory tree
+# structure needs to be modified in the future as it will
+# necessitate revision of logrotate configuration that
+# would need to be rewritten to account for the changes
+# in the structure of the log directory tree. Not to mention
+# use of underdocumented features (iliv@commandprompt.com)
+
 # *_AGE values are stored as days
 # Example: a compress value of "3" means compress files 
 # last modified more than 3 days ago.
@@ -29,11 +45,15 @@ done;
 # Delete all Evergreen log files that are older than DELETE_AGE
 # days, except for the Apache access log and Evergreen activity log.
 
-/usr/bin/find /var/log/evergreen -type f -ctime +$DELETE_AGE \
+/usr/bin/find /var/log/evergreen -type f -mtime +$DELETE_AGE \
     -not -name "ap_access*" -not -name "activity*" -delete
 
 # Compress Evergreen log files older than COMPRESS_AGE days
 
-/usr/bin/find /var/log/evergreen/ -type f -ctime +$COMPRESS_AGE \
+/usr/bin/find /var/log/evergreen/ -type f -mtime +$COMPRESS_AGE \
     -not -name "*bz2" -exec /bin/bzip2 {} \;
 
+# clean up empty directories
+
+/usr/bin/find /var/log/evergreen/ -type d -empty -exec rmdir '{}' \;
+