Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux logrotate Command: Log Dump (Rotation)

logrotate is a utility in Linux used to manage log files by automatically rotating, compressing, removing, and mailing system log files. It is a valuable tool to ensure that log files don't grow indefinitely and consume excessive disk space. In this tutorial, we will explain how to use the logrotate command to manage log files.

1. Configuration Files

logrotate is typically configured through its main configuration file, /etc/logrotate.conf, and additional configuration files in the /etc/logrotate.d/ directory. These files define the rotation policies, such as the frequency of rotation, the number of rotated files to keep, and compression settings.

2. Create a Custom Configuration

You can create a custom configuration for an application by creating a new file in the /etc/logrotate.d/ directory. For example, let's create a configuration file for a custom application called myapp:

sudo nano /etc/logrotate.d/myapp

Add the following content to the file:

/var/log/myapp/*.log {
    daily
    rotate 7
    compress
    missingok
    notifempty
    create 0640 myapp myapp
}

In this example:

  • /var/log/myapp/*.log specifies the log files to rotate.
  • daily rotates the log files daily.
  • rotate 7 keeps the last seven rotated log files.
  • compress compresses the rotated log files using gzip.
  • missingok ignores the absence of log files without raising an error.
  • notifempty skips rotation if the log file is empty.
  • create 0640 myapp myapp creates a new log file with the specified permissions (0640), owner (myapp), and group (myapp) after rotation.

Save and close the file.

3. Test Configuration

Before enabling the new configuration, it's a good idea to test it. Run logrotate in debug mode with the -d option:

sudo logrotate -d /etc/logrotate.d/myapp

Examine the output to ensure that logrotate processes the log files as intended. If there are no issues, proceed to the next step.

4. Run logrotate Manually

To manually run logrotate with your custom configuration, use the following command:

sudo logrotate /etc/logrotate.d/myapp

This command forces logrotate to process the log files according to the configuration you created. Note that running logrotate manually might result in multiple rotations in a day, depending on the rotation frequency specified in your configuration.

5. Automatic Rotation

logrotate is usually run automatically via a scheduled job defined in /etc/cron.daily/logrotate (or /etc/cron.weekly/logrotate). The system automatically processes the log files according to the configuration files found in /etc/logrotate.d/.

In conclusion, the logrotate utility is an essential tool for managing log files in Linux. It allows you to define custom rotation policies to ensure that log files don't grow indefinitely and consume excessive disk space. By following this tutorial, you can create custom logrotate configurations to effectively manage your log files.

  1. logrotate command examples:

    The logrotate command is used for log file rotation. Here are some examples:

    • Manually force log rotation:

      logrotate -f /etc/logrotate.conf
      
    • Rotate a specific log file:

      logrotate /etc/logrotate.d/mylog
      
    • View the next rotation date for a log file:

      logrotate -d /etc/logrotate.conf
      
  2. How to use logrotate in Linux:

    logrotate is a system utility for managing log files. It can be configured through a main configuration file (/etc/logrotate.conf) or individual log file configurations in the /etc/logrotate.d/ directory.

  3. Log rotation with logrotate in Linux:

    Log rotation is the process of switching out old log files to manage disk space efficiently. logrotate facilitates this by compressing, renaming, and optionally deleting old log files.

    Example configuration file:

    /var/log/mylog {
        rotate 5
        daily
        compress
        missingok
        notifempty
    }
    
  4. Customize logrotate settings in Linux:

    Customize log rotation settings based on your needs. Common settings include rotation frequency (daily, weekly), number of rotations (rotate), compression (compress), handling missing log files (missingok), and not rotating empty files (notifempty).

  5. Rotate and compress logs with logrotate:

    Include the compress option in the logrotate configuration to compress rotated log files.

    Example configuration:

    /var/log/mylog {
        rotate 5
        daily
        compress
        missingok
        notifempty
    }
    
  6. logrotate daily vs weekly rotation:

    Choose between daily and weekly log rotation based on your preference or system requirements. Use the daily or weekly directive in the logrotate configuration.

    Example daily rotation:

    /var/log/mylog {
        rotate 5
        daily
        compress
        missingok
        notifempty
    }
    

    Example weekly rotation:

    /var/log/mylog {
        rotate 5
        weekly
        compress
        missingok
        notifempty
    }
    
  7. logrotate size-based rotation:

    Rotate logs based on size by using the size directive. This ensures rotation when the log file reaches a specified size.

    Example configuration:

    /var/log/mylog {
        size 100M
        rotate 5
        compress
        missingok
        notifempty
    }
    
  8. logrotate postrotate script example:

    Execute custom scripts after log rotation using the postrotate directive. This is useful for tasks like restarting services that write to log files.

    Example configuration with a postrotate script:

    /var/log/mylog {
        rotate 5
        daily
        compress
        missingok
        notifempty
        postrotate
            /bin/systemctl restart myservice
        endscript
    }