some people managed to get on the moon, i managed to limit log files to x lines. monthly.

both processes were complicated.

one would expect tail to do the job.

but tail -n 100 filename.log > filename.log

would write „“ (nothing) into filename.log if filename.log has less than 100 lines.

the trick is to write to a new file, than move new to old file.

# test run, it will not output anything
test -x /usr/sbin/cron-apt && /usr/sbin/cron-apt

# how to truncate shrink log files

mkdir /scripts

echo "#! /bin/sh
FILENAME=$1;
MAXLINES=$2;
tail -n $MAXLINES $FILENAME > $FILENAME'_new';
mv $FILENAME'_new' $FILENAME;" > /scripts/shrinklog.sh

chmod 755 /scripts/shrinklog.sh

# shrink log to 100 lines
/scripts/shrinklog.sh /usr/sbin/cron-apt 100;

# check what it did
cat /var/log/cron-apt/log
admin