Last active
August 20, 2022 08:01
-
-
Save nathants/72968aaa7d9ab7c008fe32e399426d2c to your computer and use it in GitHub Desktop.
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
#!/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