Skip to content

Instantly share code, notes, and snippets.

@jkanclerz
Last active July 20, 2022 09:02
Embed
What would you like to do?
Simple bash log rotate script
#!/bin/bash
checkSizeOver() {
typeset -i LFSB LFSM LOG_SIZE=10
LF=$1
LOG_SIZE=$2
LFSB=$(stat -c "%s" $LF)
# This is bytes - turn into MB, base 2}
LFSM=${LFSB}/1048576
# This is bytes - turn into MB, base 2
if [ $LFSM -gt $LOG_SIZE ]
then
return 0
else
return 1
fi
}
LOG_PATH=$1
OLD_LOG_PATH=$2
for file in $LOG_PATH/*.log
do
SIZE=10
NAME=$(basename $file)
now="$(date +'%Y-%m-%d_%H-%m')"
if checkSizeOver $file $SIZE;
then
DEST=$OLD_LOG_PATH/$NAME.$now
mv $file $DEST
echo Logfile $file is greater than $SIZE MB
else
echo Logfile $file is less than $SIZE MB
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment