Operations 21 min read

Comprehensive Guide to Logrotate: Configuration, Usage, and Custom Scripts for Linux Log Management

This article provides an in‑depth tutorial on using the Linux logrotate utility, covering default configuration files, command‑line options, example configurations for services like Nginx, PHP‑FPM, Tomcat, custom Bash and Python rotation scripts, troubleshooting tips, and how to adjust cron scheduling for automated log rotation.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Comprehensive Guide to Logrotate: Configuration, Usage, and Custom Scripts for Linux Log Management

Logrotate is a Linux log file management tool that rotates, compresses, and removes old logs to save disk space.

The main configuration file is /etc/logrotate.conf and additional per‑service configs reside in /etc/logrotate.d/ .

Logrotate runs daily via /etc/cron.daily/logrotate ; the script calls /usr/sbin/logrotate with the main config.

Common command‑line options include -d (debug), -f (force), -v (verbose), and others for mail, state, etc.

Example Nginx configuration in /etc/logrotate.d/nginx shows daily rotation, keeping 7 files, using dateext , and a postrotate script that reloads Nginx.

# /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
    daily
    rotate 7
    missingok
    notifempty
    dateext
    sharedscripts
    postrotate
        if [ -f /usr/local/nginx/logs/nginx.pid ]; then
            kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
        fi
    endscript
}

Similar snippets are provided for php‑fpm, Tomcat, system logs, and a custom logrotate-nginx.sh shell script that moves, renames, compresses, and cleans up old logs.

#!/bin/bash
mkdir -p /data/nginx_logs/days
/usr/sbin/logrotate -vf /etc/logrotate.d/nginx
time=$(date -d "yesterday" +"%Y-%m-%d")
cd /data/nginx_logs/days
for i in $(ls ./ | grep "^\(.*\)\.[[:digit:]]$"); do
    mv ${i} ./$(echo ${i}|sed -n 's/^\(.*\)\.\([[:digit:]]\)$/\1/p')-${time}
 done
for i in $(ls ./ | grep "^\(.*\)-[[:digit:]-]\+$"); do
    tar jcvf ${i}.bz2 ./${i}
    rm -rf ./${i}
 done
find /data/nginx_logs/days/* -name "*.bz2" -mtime 7 -type f -exec rm -rf {} \;

Python and shell scripts are also shown for generic log rotation, illustrating how to move yesterday’s logs into date‑based directories and trigger service restarts.

#!/usr/bin/env python
import datetime, os, shutil
log_path = '/opt/jumpserver/logs/'
log_file = 'jumpserver.log'
yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
os.makedirs(os.path.join(log_path, yesterday.strftime('%Y'), yesterday.strftime('%m')))
shutil.move(log_path+log_file,
    os.path.join(log_path, yesterday.strftime('%Y'), yesterday.strftime('%m'),
    f"{log_file}_{yesterday.strftime('%Y%m%d')}.log"))
os.popen("sudo /opt/jumpserver/service.sh restart")

Finally, the article explains how to adjust the default cron timing (via /etc/anacrontab ) to run logrotate at a desired hour, and provides a simple gzip‑based cleanup script for old log files.

pythonLinuxCronSystem AdministrationLog Managementshell scriptinglogrotate
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.