Using logrotate command (with examples)
- Linux
- November 5, 2023
Example: Trigger a run manually
logrotate path/to/logrotate.conf --force
Motivation
The --force
option is used to trigger a manual run of the logrotate command. This is helpful when changes have been made to the logrotate configuration file and you want to immediately apply those changes without waiting for the next scheduled rotation.
Explanation
The --force
option forces logrotate to rotate the logs specified in the configuration file immediately, regardless of whether they have reached their rotation criteria. By default, logrotate runs based on a schedule defined in the configuration file or a system cron job.
Example Output
If logrotate successfully rotates the logs, it will display the relevant information in the terminal. For example:
rotating log /path/to/logfile1, log not empty, renaming to /path/to/logfile1.1
rotating log /path/to/logfile2, log not empty, renaming to /path/to/logfile2.1
...
Example: Run using a specific command to mail reports
logrotate path/to/logrotate.conf --mail /usr/bin/mail_command
Motivation
The --mail
option allows you to specify a command that will be used to send logrotate reports via email. This is useful if you want to customize how the reports are sent or if you want to use a different mail command than the default one.
Explanation
The --mail
option followed by the command (/usr/bin/mail_command
in this example) instructs logrotate to use the specified command to send the logrotate reports via email. The command should be the path to the mail command or its equivalent, which is responsible for sending emails on the system.
Example Output
The output generated by the mail command will depend on the specific implementation. It may include information about the success or failure of the log rotation process, as well as any relevant error messages or log entries.
Example: Run without using a state (lock) file
logrotate path/to/logrotate.conf --state /dev/null
Motivation
The --state
option allows you to specify a path to an alternative state file for logrotate. By using /dev/null
as the state file, logrotate will not create or update a state file at all. This can be useful in situations where you don’t want to track the state of log rotations or you want to disable the state file functionality.
Explanation
The --state
option followed by /dev/null
tells logrotate not to create or update a state file. The state file is used to keep track of when logs were last rotated, allowing logrotate to determine which logs need to be rotated based on their rotation criteria.
Example Output
There will be no output related to the state file, as it is not being used. However, logrotate will still perform the log rotation according to the configuration file provided.
Example: Run and skip the state (lock) file check
logrotate path/to/logrotate.conf --skip-state-lock
Motivation
The --skip-state-lock
option skips the state file check, allowing logrotation to proceed even if another instance of logrotate is already running. This can be useful in scenarios where you want to forcefully rotate logs regardless of whether another logrotate process is already running.
Explanation
The --skip-state-lock
option tells logrotate to proceed with the log rotation even if it detects a lock file associated with another logrotate process. By default, logrotate checks for a lock file and exits if it finds one, preventing concurrent log rotation processes.
Example Output
There will be no specific output related to the state file check. If logrotate successfully rotates the logs, it will display the rotation information as usual.
Example: Tell logrotate to log verbose output into the log file
logrotate path/to/logrotate.conf --log path/to/log_file
Motivation
The --log
option allows you to specify the path to a log file where the verbose output of logrotate will be logged. This can be useful for debugging or monitoring purposes, as it provides detailed information about the log rotation process.
Explanation
The --log
option followed by the path to a log file (path/to/log_file
in this example) instructs logrotate to log verbose output into the specified file. The verbose output includes details such as which logs were rotated, any errors encountered, and the actions taken during the rotation process.
Example Output
The output generated by logrotate will be logged into the specified log file. For example:
rotating log /path/to/logfile1, log not empty, renaming to /path/to/logfile1.1
rotating log /path/to/logfile2, log not empty, renaming to /path/to/logfile2.1
...
This output will be written into the log file specified with the --log
option.