Last active
December 20, 2015 19:39
-
-
Save weisjohn/6185194 to your computer and use it in GitHub Desktop.
rotate logs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# rotates logs for all services on this box | |
# the services for which we should rotate logs | |
SERVICE_DIR=~/mysrc | |
services=$(ls $SERVICE_DIR) | |
function rotate { | |
logfile=$SERVICE_DIR/$1/logs/$2.log | |
if [ ! -f "$logfile" ]; then | |
# echo "log file not found $logfile" | |
return | |
fi | |
# make a timestamp | |
timestamp=`date +%Y-%m-%d` | |
newlog=$logfile.$timestamp | |
# move the file and touch the default again | |
mv $logfile $newlog | |
touch $logfile | |
} | |
function expire { | |
logpath=$SERVICE_DIR/$1/logs | |
if [ ! -d "$logpath" ]; then | |
# echo "logpath not found $logfile" | |
return | |
fi | |
# find files that are older than 15 days | |
find $logpath -mtime +15 -exec rm -f \{} \; | |
} | |
for service in $services; do | |
expire $service | |
for file in "access" "error"; do | |
rotate $service $file | |
done; | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment