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

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin