logrotate – Rotate, compress and delete system logs

The logrotate command is a Linux utility tool that is used to manage the system logs. It is used to rotate, compress, and delete the system logs to ensure that the system does not run out of disk space. The logrotate command is usually executed as a daily cron job, and it ensures that the log files are rotated according to a predefined schedule.

Overview

The logrotate command is used to manage log files in Linux systems. It can rotate, compress, and delete log files to free up disk space. The command reads the configuration file /etc/logrotate.conf and rotates the log files based on the configurations set in the file.

The logrotate command can also be used to rotate logs for specific applications. The configuration file for an application can be placed in the /etc/logrotate.d/ directory. The configuration file should specify the log files to be rotated, the frequency of rotation, and the actions to be taken after rotation.

Examples

Here are some examples of how to use the logrotate command:

  1. To rotate the logs for all applications, run the following command:
sudo logrotate -f /etc/logrotate.conf
  1. To rotate the logs for a specific application, create a configuration file for the application in the /etc/logrotate.d/ directory. Here is an example configuration file for Apache:
/var/log/httpd/*log {
    weekly
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

This configuration file specifies that the logs for Apache should be rotated weekly, compressed, and kept for up to 52 weeks.

Options

The logrotate command has several options that can be used to modify its behavior. The following table lists the available options:

Option Description
-d, –debug Enable debug mode
-f, –force Force log rotation
-h, –help Display help information
-s, –state Specify the state file to use
-v, –verbose Enable verbose mode
-version Display version information

Troubleshooting tips

Here are some troubleshooting tips for common issues with the logrotate command:

  • If the logs are not being rotated as expected, check the configuration file for errors. The configuration file should be located in /etc/logrotate.conf or /etc/logrotate.d/.
  • If the logs are being rotated but not compressed, check that the compress option is specified in the configuration file.
  • If the logs are being compressed but not deleted, check that the rotate option is specified in the configuration file.

Notes

  • The logrotate command is usually executed as a daily cron job.
  • The logrotate command can be used to manage logs for specific applications by creating a configuration file in the /etc/logrotate.d/ directory.
  • The logrotate command is a useful tool for managing disk space on Linux systems by rotating, compressing, and deleting log files.