Skip to content

Instantly share code, notes, and snippets.

@hemanth22
Forked from jkanclerz/log_rotate.sh
Created October 3, 2020 10:01
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 hemanth22/d7098edef49ad572e0812d43212a13f3 to your computer and use it in GitHub Desktop.
Save hemanth22/d7098edef49ad572e0812d43212a13f3 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