Skip to content

Instantly share code, notes, and snippets.

@jkanclerz
Last active May 9, 2023 11:22
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jkanclerz/b878617860b66a070215427e92f1683f to your computer and use it in GitHub Desktop.
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