rotatelogs Command in Linux
The rotatelogs command in Linux is a utility used to manage log rotation for Apache HTTP Server logs. It allows administrators to rotate log files based on time intervals or file sizes, ensuring that log files do not grow indefinitely and consume excessive disk space.
The rotatelogs command is particularly useful for maintaining organized and manageable log files, which are essential for monitoring and troubleshooting server activities.
Table of Contents
Here is a comprehensive guide to the options available with the rotatelogs command −
- Understanding rotatelogs Command
- Installation of rotatelogs Command
- Syntax of rotatelogs Command
- rotatelogs Command Options
- Examples of rotatelogs Command in Linux
- Combining with Other Tools
Understanding rotatelogs Command
This command is particularly useful for maintaining organized and manageable log files, which are essential for monitoring and troubleshooting server activities.
By using rotatelogs, administrators can specify the rotation criteria, such as rotating logs every hour or when they reach a certain size, and can also configure additional options like using local time, creating hard links to the current log file, and executing custom scripts on log rotation. This flexibility makes rotatelogs a valuable tool for efficient log management in Apache HTTP Server environments.
Installation of rotatelogs Command
Letâs install it −
sudo apt install apache2-utils

The rotatelogs command is typically used in conjunction with Apache's piped logging feature. It supports rotation based on a specified time interval or maximum file size.
rotatelogs

Syntax of rotatelogs Command
The basic syntax of the rotatelogs command is as follows −
rotatelogs [options] logfile rotationtime|filesize [offset]
Here,
- options − Various options to control the behavior of the command.
- logfile − The base name of the log file.
- rotationtime|filesize − The time interval (in seconds) or maximum file size (in bytes, kilobytes, megabytes, or gigabytes) for log rotation.
- offset − The number of minutes offset from UTC (optional).
rotatelogs Command Options
Here are some commonly used options with the rotatelogs command −
Options | Description |
---|---|
-l | Use local time rather than GMT for the interval or strftime formatting. |
-L linkname | Create a hard link from the current log file to the specified link name. |
-p program | Execute the specified program every time a new log file is opened. |
-f | Open the log file immediately when rotatelogs starts. |
-t | Truncate the log file instead of rotating it. |
-v | Produce verbose output on STDERR. |
-e | Echo logs through to stdout. |
-c | Create a log file for each interval, even if empty. |
Examples of rotatelogs Command in Linux
Let's explore some examples of using the rotatelogs command with detailed explanations.
- Rotate Logs Based on Time Interval
- Rotate Logs Based on File Size
- Use Local Time for Log Rotation
- Create a Hard Link to the Current Log File
- Execute a Program on Log Rotation
- Open Log File Immediately
- Truncate Log File Instead of Rotating
- Produce Verbose Output
- Echo Logs Through to stdout
- Create Log File for Each Interval, Even if Empty
Rotate Logs Based on Time Interval
To rotate logs every hour, use the following command −
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 3600" combined

This command configures Apache to rotate the access log file every hour. The log file name includes the date and time of rotation, ensuring unique log file names.
Rotate Logs Based on File Size
To rotate logs when they reach a size of 100 megabytes, use the following command −
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 100M" combined

This command configures Apache to rotate the access log file when it reaches 100 megabytes in size. The log file name includes the date and time of rotation.
Use Local Time for Log Rotation
To use local time rather than GMT for log rotation, use the -l option −
CustomLog "|/usr/sbin/rotatelogs -l /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 3600" combined

This command configures Apache to rotate the access log file every hour using local time for the interval.
Create a Hard Link to the Current Log File
To create a hard link from the current log file to a specified link name, use the -L option −
CustomLog "|/usr/sbin/rotatelogs -L /var/log/apache2/current_access_log /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 3600" combined

This command configures Apache to create a hard link named current_access_log that points to the current log file. This allows continuous monitoring of the log file across rotations.
Execute a Program on Log Rotation
To execute a specified program every time a new log file is opened, use the -p option −
CustomLog "|/usr/sbin/rotatelogs -p /usr/local/bin/log_processor /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 3600" combined

This command configures Apache to execute the log_processor program every time a new log file is opened. The filename of the newly opened file is passed as the first argument to the program.
Open Log File Immediately
To open the log file immediately when rotatelogs starts, use the -f option −
CustomLog "|/usr/sbin/rotatelogs -f /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 3600" combined

This command configures Apache to open the log file immediately when rotatelogs starts, rather than waiting for the first log entry.
Truncate Log File Instead of Rotating
To truncate the log file instead of rotating it, use the -t option −
CustomLog "|/usr/sbin/rotatelogs -t /var/log/apache2/access_log 3600" combined

This command configures Apache to truncate the log file every hour instead of rotating it. No suffix is added to the filename.
Produce Verbose Output
To produce verbose output on STDERR, use the -v option −
CustomLog "|/usr/sbin/rotatelogs -v /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 3600" combined

This command configures Apache to produce verbose output, including the result of configuration parsing and all file open and close actions.
Echo Logs Through to stdout
To echo logs through to stdout, use the -e option −
CustomLog "|/usr/sbin/rotatelogs -e /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 3600" combined

This command configures Apache to echo logs through to stdout, allowing further real-time processing by another tool in the chain.
Create Log File for Each Interval, Even if Empty
To create a log file for each interval, even if empty, use the -c option −
CustomLog "|/usr/sbin/rotatelogs -c /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 3600" combined

This command configures Apache to create a log file for each interval, even if no data is logged during that interval.
Combining with Other Tools
You can combine rotatelogs with other tools to further process log files. For example, you can use gzip to compress rotated log files −
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 3600 | gzip > /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S.gz" combined
This command configures Apache to rotate the access log file every hour and compress the rotated log files using gzip.
Using rotatelogs with Custom Scripts
You can use custom scripts to perform additional actions on log rotation. For example, you can use a script to upload rotated log files to a remote server −
CustomLog "|/usr/sbin/rotatelogs -p /usr/local/bin/upload_logs /var/log/apache2/access_log.%Y-%m-%d-%H_%M_%S 3600" combined
This command configures Apache to execute the upload_logs script every time a new log file is opened.
Conclusion
The rotatelogs command is a versatile and powerful tool for managing log rotation in Apache HTTP Server. By understanding its various options and how to use them, you can effectively maintain organized and manageable log files, ensuring that your server's logging system remains efficient and reliable.
Whether you're a system administrator or a network engineer, mastering the rotatelogs command will enhance your ability to monitor and troubleshoot server activities.