Skip to content

## ๐Ÿ“ Author

Birat Aryal โ€” birataryal.github.io
Created Date: 2025-06-23
Updated Date: Monday 23rd June 2025 07:36:09
Website - birataryal.com.np
Repository - Birat Aryal
LinkedIn - Birat Aryal
DevSecOps Engineer | System Engineer | Cyber Security Analyst | Network Engineer

Log Rotation Configuration for Servicename

This file defines the log rotation policy for the Servicename service. The configuration is located at:

Text Only
/etc/logrotate.d/Servicename

๐Ÿ“„ Configuration Overview

Bash
<location of log>/*.log {
    daily
    create <Permission with umask> <username> <group>
    dateext
    compress
    ifempty
    missingok
    copytruncate
    rotate 7
    postrotate
        # Remove rotated files older than 7 days
        find <location of logs> -type f -mtime +7 -name '*.gz' -delete
    endscript
}

๐Ÿ”ง Explanation of Options

Directive Description
<location of log>/*.log Path to log files that will be rotated (e.g., /var/log/servicename/*.log)
daily Rotate logs on a daily basis
create Create a new empty log file after rotation with specific permissions and ownership. Replace <Permission with umask>, <username>, and <group> accordingly (e.g., 0640 servicename adm)
dateext Adds the current date to the rotated log filenames instead of a numerical suffix
compress Compresses the old log files using gzip
ifempty Rotate log files even if they are empty
missingok Don't show an error if the log file is missing
copytruncate Truncates the original log file after copying it, used for active logs written by running processes
rotate 7 Keep the last 7 rotated log files before deletion
postrotate ... endscript Executes a script after rotation; here it deletes compressed log files (.gz) older than 7 days

๐Ÿงน Log Cleanup

After each rotation, this command will delete compressed (*.gz) log files older than 7 days:

Bash
find <location of logs> -type f -mtime +7 -name '*.gz' -delete

Replace <location of logs> with the correct log directory path (e.g., /var/log/servicename).


โœ… Example

If your logs are located at /var/log/servicename/ and owned by user servicename in group adm, an example config might look like:

Bash
/var/log/servicename/*.log {
    daily
    create 0640 servicename adm
    dateext
    compress
    ifempty
    missingok
    copytruncate
    rotate 7
    postrotate
        find /var/log/servicename -type f -mtime +7 -name '*.gz' -delete
    endscript
}

๐Ÿ› ๏ธ Apply and Test

To test this configuration manually:

Bash
logrotate -d /etc/logrotate.d/Servicename   # Dry run
logrotate -f /etc/logrotate.d/Servicename   # Force rotation