Master Linux Log Rotation with Logrotate: Configs, Scripts, and Common Fixes
This guide explains how to use Logrotate on Linux for automatic log file rotation, covering default configuration files, key options, example setups for Nginx, PHP, Tomcat, system logs, custom shell and Python scripts, cron scheduling, and troubleshooting techniques to ensure logs are rotated reliably.
Logrotate is a built‑in Linux utility that automates log file rotation, compression, and removal to save disk space and keep logs organized.
1. Configuration Files Overview
The main configuration resides in /etc/logrotate.conf, while individual service configurations are placed in /etc/logrotate.d/ and automatically included.
/etc/logrotate.conf
/etc/logrotate.d/Settings in logrotate.conf apply globally unless overridden by files in /etc/logrotate.d/. Logrotate runs daily via the script /etc/cron.daily/logrotate, which invokes /usr/sbin/logrotate /etc/logrotate.conf.
2. Common Options
daily, weekly, monthly – rotation frequency. rotate <em>n</em> – keep the last n rotated files. compress / nocompress – gzip the rotated logs. dateext – append the current date to rotated filenames. missingok – ignore missing log files. notifempty – skip rotation if the log is empty. copytruncate – copy the log and truncate the original (useful for files still open by a process). sharedscripts – run postrotate scripts once after all logs are processed. postrotate … endscript – commands executed after rotation (e.g., reload a service).
3. Example Service Configurations
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
}PHP
/Data/logs/php/*log {
daily
rotate 365
missingok
notifempty
compress
dateext
sharedscripts
postrotate
if [ -f /Data/app/php5.6.26/var/run/php-fpm.pid ]; then
kill -USR1 `cat /Data/app/php5.6.26/var/run/php-fpm.pid`
fi
endscript
}Tomcat
/Data/app/tomcat-7-huanqiu/logs/catalina.out {
daily
rotate 14
copytruncate
compress
notifempty
missingok
}System Logs
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure {
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid`
endscript
}4. Custom Rotation Scripts
When Logrotate’s default schedule (usually early morning) does not meet requirements, you can create custom scripts and schedule them with cron:
# Example: force rotation of nginx logs at 23:59 daily
59 23 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx >/dev/null 2>&1Or adjust the cron daemon timing by editing /etc/anacrontab to change the START_HOURS_RANGE and RANDOM_DELAY values.
5. Python and Shell Helpers
Simple Python scripts can move yesterday’s logs into date‑based directories and optionally restart services:
#!/usr/bin/env python
import datetime, os, shutil, sys
log_path = '/opt/jumpserver/logs/'
log_file = 'jumpserver.log'
yesterday = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y%m%d')
os.makedirs(os.path.join(log_path, yesterday[:4], yesterday[4:6]), exist_ok=True)
shutil.move(log_path + log_file,
f"{log_path}{yesterday[:4]}/{yesterday[4:6]}/{log_file}_{yesterday}.log")
os.system('sudo /opt/jumpserver/service.sh restart')A generic shell function can rotate any log file, keep a configurable number of days, and delete older copies:
rotate() {
log=$1
cp $log ${log}.$(date -d "yesterday" +%Y%m%d)
rm -f ${log}.$(date -d "7 days ago" +%Y%m%d)
}
for f in "$@"; do rotate $f; done6. Troubleshooting Logrotate Failures
If logs are not rotating, verify that the /etc/cron.daily/logrotate script runs without errors. Replace the default logger call with -f to force rotation during manual testing:
# /etc/cron.daily/logrotate
/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -f logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0Restart the cron service after modifications:
/etc/init.d/crond restart7. Additional Utilities
Shell scripts can compress old logs, delete files older than a retention period, or rename logs with timestamps. Example compression script:
#!/bin/bash
logdir=/var/log/fss/nginx
find $logdir -name "*.log" -mtime +3 -exec gzip {} \;These utilities complement Logrotate when custom retention policies are needed.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
