Logrotate is a system utility that automates the process of rotating, compressing, and deleting log files. It’s typically run on a scheduled basis to keep log files from growing too large and consuming excessive disk space. However, there are situations where you might want to run logrotate manually for troubleshooting purposes or to apply specific configurations immediately. In this tutorial, we’ll explore how to run logrotate manually.
Understanding Logrotate Configuration
Logrotate uses configuration files to determine which log files to rotate, when to rotate them, and what actions to take after rotation (such as compression or mailing). The primary configuration file is usually located at /etc/logrotate.conf
, but specific configurations for services like Apache, MySQL, or Nginx are often found in separate files within the /etc/logrotate.d/
directory.
Running Logrotate Manually
To run logrotate manually, you can use the logrotate
command followed by options that specify how it should operate. Here are some key options:
-
-d or –debug: This option enables debug mode, where logrotate will describe what actions it would take without actually rotating any logs. It’s useful for testing configurations.
Example:
logrotate -d /etc/logrotate.d/nginx
-
-f or –force: This option forces logrotate to rotate logs even if the conditions specified in the configuration file (like the size threshold) are not met. It’s useful for testing purposes or when you need to ensure logs are rotated immediately.
Example:
logrotate -f /etc/logrotate.d/nginx
-
-v or –verbose: This option increases verbosity, providing more detailed output about what logrotate is doing. It can be useful for troubleshooting or understanding the rotation process in more detail.
Example (combining with force):
logrotate -vf /etc/logrotate.conf
Choosing the Right Configuration File
When running logrotate manually, you need to specify which configuration file to use. If you want to apply global settings and include all service-specific configurations, you can use the main /etc/logrotate.conf
file:
logrotate -f /etc/logrotate.conf
However, if you’re interested in rotating logs for a specific service (like Nginx), it’s often more convenient and targeted to use the configuration file dedicated to that service:
logrotate -f /etc/logrotate.d/nginx
Keep in mind that when using a service-specific configuration file directly, any global settings defined in /etc/logrotate.conf
will not be applied automatically. You should ensure that all necessary options are specified within the service’s configuration file.
Best Practices
- Always back up critical log files before manually rotating them to prevent loss of important data.
- Use debug mode (
-d
) first to understand what actions logrotate will take without actually modifying any logs. - Be cautious with the
--force
option, as it can lead to unintended rotations or deletions if not used carefully.
By following these guidelines and examples, you should be able to run logrotate manually for your specific needs, whether it’s for troubleshooting, applying configurations immediately, or managing logs for a particular service.