In any Linux system running chatty / verbose logging can grow its log files very quickly. Left unchecked, they have the potential to consume available filesystem space and disrupt normal operations
A handy lightweight utility called “logrotate” can be installed to automate the log trimming, compression, and retention tasks eliminating manual intervention or custom shell scripts
In this guide we’ll cover installation on a Debian based Linux system and a simple configuration example.
1. Installation
sudo apt update
sudo apt install logrotateOnce that’s installed, check the version and the man pages to understand basic operation and capabilities.
$ logrotate --version
$ man logrotate2. Configuration
Create a new “logrotate” config file. Name the file based on your projects naming conventions
cd /etc/logrotate.d/Using an editor, insert the below configurations into your project filename. In this case my config file is called “dispatch_scada“
sudo vim dispatch_scadaInsert / paste this configuration and modify it to meet the technical needs.
/{your_path_to_lof_file}/dispatch_scada_loader.log {
size 2500M
rotate 5
compress
missingok
notifempty
copytruncate
}2.1 Config Description
{your_path_to_lof_file}= Directory path to log filesize 250M= rotates when log exceeds 10MBrotate 5= keeps 5 old logs (.1.gz, .2.gz, etc.)compress= gzips old logs to save spacemissingok= skips rotation if log is missingnotifempty= skips rotation if log is emptycopytruncate= safely rotates logs even if your script keeps the file ope
2.2 Config Test
Dry run:
sudo logrotate -d /etc/logrotate.d/dispatch_scadaForce rotation:
sudo logrotate -f /etc/logrotate.d/dispatch_scadaCheck rotated logs:
ls -lh /{your_path_to_lof_file}/dispatch_scada_loader.log*2.3. Add to Cron
Set “cron" to run at 2AM daily
sudo crontab -e
0 2 * * * /usr/sbin/logrotate /etc/logrotate.d/dispatch_scada > /dev/null 2>&1Following this “logrotate” process should keep your log files maintained and your system not suffering due to lack of filesystem space.