Skip to content

Instantly share code, notes, and snippets.

@nathants
Last active August 20, 2022 08: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 nathants/72968aaa7d9ab7c008fe32e399426d2c to your computer and use it in GitHub Desktop.
Save nathants/72968aaa7d9ab7c008fe32e399426d2c to your computer and use it in GitHub Desktop.
#!/bin/bash
# The MIT License (MIT)
# Copyright (c) 2022-present Nathan Todd-Stone
# https://en.wikipedia.org/wiki/MIT_License#License_terms
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