Created
August 2, 2017 15:19
-
-
Save lcarsos/b16304a1909d1e41e8ef5b7fdb8b82c3 to your computer and use it in GitHub Desktop.
A simple bash function that imitates the functionality of log rotate
This file contains hidden or 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
#!/usr/bin/env bash | |
# Immitates log rotate. | |
# You should change maxsize and rotatemax to your own use case. | |
rotate() { | |
local file=$1 | |
local rotatemax=9 # The max number of numbered files to keep around | |
local maxsize=2097152 # After the main file passes this threshold rotate occurs | |
local filesize=$(wc -c $file | cut -f 1 -d ' ') | |
if [[ $filesize -gt $maxsize ]]; then | |
if [[ -s ${file}.1 ]]; then | |
gzip ${file}.1 | |
fi | |
for i in $(eval echo {$rotatemax..1}); do | |
if [[ -s ${file}.${i}.gz ]]; then | |
mv ${file}.${i}.gz ${file}.$(( i + 1 )).gz | |
fi | |
done | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment