Hero Image
- Mihai Surdeanu

Use logrotate to automatically manage your logfiles

Currently, I am using logrotate on all my VPS servers to automatically manage my logfiles and to avoid keeping them indefinitely. What's the potential problem by keeping them forever? Quite simple and intuitive. At some point, my servers will run out of free disk space and all applications deployed there will be impacted.

Having this in mind, having a tool for automatically manage logfiles is absolutely necessary. Logrotate is a system utility tool specialized in automatic rotation and compression of logs files.

This tool is by default installed in all Ubuntu distributions. To double check if you already have this tool installed, you have to use the following command to see it's current version: logrotate.

If the tool is installed, you will see something similar with next output:

logrotate 3.14.0 - Copyright (C) 1995-2001 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU General Public License

Usage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail=command]
        [-s|--state=statefile] [-v|--verbose] [-l|--log=logfile] [--version]
        [-?|--help] [--usage] [OPTION...] <configfile>

Let's say we have a script that generates hourly logs in a file called output.log, file present under directory: /home/user/logs. Without logrotate, this file will become bigger and bigger in time and at some point everything will crash. To fix this approach, we would like to implement a way to keep just logs generated in last X days and to compress old logs to reduce as much as possible the amount of disk space used. Fair enough.

How we can achieve this? Logrotate. The easiest way to achieve it is to modify global configuration file: /etc/logrotate.conf:

/home/user/logs/*.log {
    rotate 14

The logs will be rotated daily, only last 14 days will be took into consideration and compression is also activated. missingok is a directive to not throw any error if the log file is missing.

That's it! Feel free to use man documentation of this tool to understand all his capabilities.

Other Related Posts:

Microbenchmarking with Java

Microbenchmarking with Java

Hello guys,

Today, we are going to continue the series of articles about Java ecosystem. More specific, today, we are going to talk about JMH (Java Microbenchmark Harness).

2nd Apr 2022 - Mihai Surdeanu