Operations 13 min read

Master Log Rotation: Shell Scripts for Automated Nginx Log Splitting

This guide walks through why nginx logs need daily rotation, demonstrates manual log splitting, provides a complete shell script for automated daily log rotation, explains logrotate configuration, and shows how to test and schedule the process with cron, ensuring clean and manageable log files.

Raymond Ops
Raymond Ops
Raymond Ops
Master Log Rotation: Shell Scripts for Automated Nginx Log Splitting

Log Splitting (Shell Script)

nginx logs are not rotated by default; as a website runs for a long time the log files grow large, making single‑file handling cumbersome, so administrators usually rotate logs daily.

Manual Log Splitting

1. Send a signal to the nginx master process to reopen a new log file, which effectively rotates the log. 2. Backup the current log file by renaming it with the current date. 3. Verify the number of log entries before and after rotation.

<code># Backup original log
mv /var/log/nginx/access.log /var/log/nginx/access.log.$(date +%F)
# Send USR1 signal to nginx master to reopen logs
kill -USR1 $(ps -ef | grep nginx | grep master | awk '{print $2}')
</code>

Shell Script for Automated Rotation

<code>#!/bin/bash
# Source log directory
logs_path="/var/log/nginx"
# Backup directory named with yesterday's date
back_logs_path="${logs_path}/$(date -d 'yesterday' +'%F')"
# Create backup directory
mkdir -p ${back_logs_path}
# Rename old logs with date suffix
cd ${logs_path} && find . -type f | xargs -I {} mv {} {}.$(date -d 'yesterday' +'%F')
# Move renamed logs to backup directory
cd ${logs_path} && find . -type f | xargs -I {} mv {} ${back_logs_path}
# Signal nginx to create a new log file
kill -USR1 `cat /var/run/nginx.pid`
</code>

Using logrotate

logrotate provides a more convenient way to rotate logs automatically. A typical configuration for nginx looks like:

<code>/var/log/nginx/*.log {
    daily
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 640 nginx adm
    sharedscripts
    postrotate
        if [ -f /var/run/nginx.pid ]; then
            kill -USR1 `cat /var/run/nginx.pid`
        fi
    endscript
}
</code>

The

postrotate

block sends the USR1 signal so nginx starts writing to a new log file after rotation.

Testing the Rotation

Generate a large amount of log data using

ab

(ApacheBench) to simulate concurrent requests, then run the script or invoke

logrotate

manually to verify that logs are split and compressed as expected.

<code># Install ApacheBench
yum install httpd-tools -y
# Generate 10,000 requests with 100 concurrent users
ab -c 100 -n 10000 http://10.0.0.8/
# Force logrotate to run
logrotate -f /etc/logrotate.d/nginx
</code>

After rotation, old logs are archived (e.g.,

movie.yuchaoit.log.1

) and compressed (e.g.,

movie.yuchaoit.log.2.gz

).

Scheduling with Cron

Add a daily cron job to run the rotation script automatically:

<code>0 0 * * * /bin/bash /etc/nginx/cut_log.sh
</code>

Or schedule logrotate directly:

<code>1 0 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx >> /var/log/nginx/logrotate_nginx.log 2>&1
</code>

These steps ensure that nginx logs are rotated daily, backed up, and compressed without manual intervention.

Log backup illustration
Log backup illustration
linuxnginxCronSystem Administrationshell scriptlog rotationlogrotate
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

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.