Skip to content

Instantly share code, notes, and snippets.

@nathants nathants/rotate-logs.sh
Last active Apr 14, 2019

Embed
What would you like to do?
#!/bin/bash
set -euo pipefail
# usage: myprogram.py | rotate-logs /tmp/myprogram.log [1000000]
file=$1
max_lines=${2:-1000000}
touch $file
line_count=$(cat $file | wc -l)
while read line; do
if (($line_count > $max_lines * 2)); then
mv $file $file.old
tail -n $max_lines $file > $file
rm $file.old
line_count=$max_lines
fi
echo $line >> $file
line_count=$(($line_count + 1))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.