Report issue Add example

logrotate

Rotate, compress, and remove system logs

Description

The logrotate command is used to rotate, compress, and remove system logs, and can also send logs to a specified email address. It simplifies the management of log files generated by the system. Each log file can be configured for daily, weekly, or monthly processing, or processed immediately when it exceeds a certain size. You must specify a configuration file; the default configuration file is located at /etc/logrotate.conf.

Syntax

logrotate (options) (parameters)

Options

-?, --help: Display help information;
-d, --debug: Enable debug mode, showing detailed execution steps for troubleshooting;
-f, --force: Force log rotation even if logrotate doesn't think it's necessary;
-s, --state=<state_file>: Use the specified state file;
-v, --verbose: Display verbose information during execution;
--usage: Display basic usage information.

Parameters

Configuration file: Specify the configuration file for the logrotate command.

Examples

cron typically invokes logrotate via the script /etc/cron.daily/logrotate.

The main configuration file /etc/logrotate.conf usually includes configurations from the /etc/logrotate.d directory.

You can create a file with any suffix in /etc/logrotate.d to manage specific logs. For example:

/tmp/log/log.txt
{
    copytruncate
    daily
    rotate 30
    missingok
    ifempty
    compress
    noolddir
}

This configuration means /tmp/log/log.txt will be rotated and compressed daily, keeping 30 backups.

Configuration Options:

Precautions

If you use wildcards like this in /etc/logrotate.d:

/tmp/log/log*
{
    ...
}

Be careful, as this might cause already rotated logs to be rotated again if their filenames still match the pattern (e.g., log.txt.1).