Skip to content

Instantly share code, notes, and snippets.

@weisjohn
Last active December 20, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weisjohn/6185194 to your computer and use it in GitHub Desktop.
Save weisjohn/6185194 to your computer and use it in GitHub Desktop.
rotate logs
#!/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