What the "logrotate failed" Error Means
The logrotate failed error is a general notification from the task scheduling system (usually cron or systemd timer) that the logrotate log rotation service terminated with an error. This is not a specific error code, but rather the process exit status. The actual cause of the error is never indicated in this message. You need to look for it in the logs of logrotate itself.
Typical output in the system log (/var/log/syslog or via journalctl) might look like this:
error: error rotating /var/log/nginx/access.log: cannot open '/var/log/nginx/access.log' for reading: Permission denied
Or:
error: error creating new /var/log/nginx/access.log.1.gz (is filesystem full?): No space left on device
The logrotate configuration is usually run daily via the logrotate.timer systemd timer or a job in /etc/cron.daily/. The error means that log rotation did not occur, and old logs continue to grow, which can lead to disk space exhaustion.
Common Causes
The logrotate failed error is a symptom. Here are the most frequent root causes:
- Insufficient Permissions (Permission denied). The user under which
logrotateruns (usuallyroot) lacks permission to read the source log file, write to the archive directory, or create a new (compressed) file. Often occurs if logs are created by another user (e.g.,www-datafor a web server) and have overly restrictive permissions. - Insufficient Disk Space (No space left on device). The partition where the source logs are stored or where compressed archives (
.gz) should be saved has run out of free space.logrotatecannot create a new file or compress the old one. - Incorrect File Path in Configuration. The configuration files in
/etc/logrotate.d/specify a non-existent path to a log file or directory. - Conflicting or Syntactically Incorrect Configs. An error in one of the files in
/etc/logrotate.d/(e.g., a missing closing curly brace}) can cause the entire process to fail. - Log File Locked by Another Process. The application (nginx, Apache) or another process holds the file in an exclusive lock, and
logrotatecannot rename it (which is the first step of rotation). - Compression Utility (gzip) Problem. If compression is enabled (
compressordelaycompress) in the config, but thegziputility is missing or corrupted, rotation will fail. - Error in
postrotate/prerotateScript. If custom scripts are specified in the configuration, their execution may fail, causing the entire job to fail.
Solutions
Method 1: Analyze the Exact Error Message (Mandatory First Step)
Before fixing anything, you must find the true cause of the error.
- Check the systemd journal (if using systemd-timer):
sudo journalctl -u logrotate.service --since "1 hour ago" | grep -i error
Or simply view the entire service log:sudo journalctl -u logrotate.service -n 50 - Check the system log (if using cron.daily):
sudo grep -i "logrotate" /var/log/syslog | tail -n 50
Or, on CentOS/RHEL:sudo grep -i "logrotate" /var/log/messages | tail -n 50 - Find lines containing
error:. These contain the problem description (e.g.,Permission denied,No space left on device,error: ...).
Method 2: Check and Correct Permissions
If you see Permission denied in the logs:
- Determine which user/group the process runs as. Usually it's
root. - Check the ownership and permissions on the problematic log file and its parent directory:
ls -la /var/log/nginx/access.log ls -ld /var/log/nginx/ - Option A (Recommended): Change the log file's group to the group the application runs as (e.g.,
www-data), and grant the group write permission. Then ensurelogrotate(running as root) can read the file.sudo chown root:www-data /var/log/nginx/access.log sudo chmod 640 /var/log/nginx/access.log
Ensure the/var/log/nginx/directory is executable (+x) forroot. - Option B (Less Secure): Temporarily grant full permissions for diagnosis (do not leave it like this!):
sudo chmod 666 /var/log/nginx/access.log
After fixing the issue, restore secure permissions.
Method 3: Free Up Disk Space
If the cause is No space left on device:
- Check disk usage:
Find the filled partition (e.g.,df -h/varor/). - Manually clean up old or unnecessary logs (carefully!):
Caution: Do not delete active (current) log files without a dot (e.g.,# View largest files in /var/log sudo du -sh /var/log/* | sort -rh | head -n 10 # Clean up old compressed logs (example for nginx) sudo rm -f /var/log/nginx/*.gzaccess.log). Only delete archives (*.gz,*.1, etc.). - After cleanup, run
logrotatemanually to check if it works:sudo logrotate -f /etc/logrotate.conf
Method 4: Check and Test Configuration
- Check the syntax of all configs:
Thesudo logrotate -d /etc/logrotate.conf-dflag enables debug mode. The rotation logic will be run, but no files will actually be renamed, deleted, or compressed. This is safe. - Carefully review the output. Look for
reading config filelines (ensure all needed files from/etc/logrotate.d/are read) anderror:. A common error is a missing closing}in one of the files. - If you find a problematic config in
/etc/logrotate.d/, edit it. For a service likeapp:
Ensure the log paths exist:sudo nano /etc/logrotate.d/appsudo ls -la /var/log/app/
Method 5: Resolve File Locking Issues
If an application (like nginx) holds the file open, logrotate may fail to rename it. Use the copytruncate directive in the configuration for that specific log.
- Edit the config for the problematic service (e.g.,
/etc/logrotate.d/nginx). - For the block handling
access.loganderror.log, addcopytruncateright after{:/var/log/nginx/*.log { copytruncate # <-- Added daily missingok rotate 14 compress delaycompress notifempty create 640 root adm sharedscripts postrotate [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid` endscript }copytruncatecopies the current log to a new file and truncates the original, allowing the application to continue writing to the same descriptor. It's less atomic than the standard method (renaming) but solves locking issues.
Method 6: Force Run and Final Check
After making changes:
- Force a rotation run (with the
-fflag):
If the command exits without errors — great.sudo logrotate -f /etc/logrotate.conf - Check that archives were created:
Files likels -la /var/log/nginx/ | grep ".gz"access.log.1.gzshould appear. - Check that a new log file was created and is empty (or has size 0):
sudo ls -la /var/log/nginx/access.log - Wait for the next scheduled run (or check in a day) and review system logs again for errors.
Prevention
- Regular disk space monitoring. Add checks for
/varto your monitoring systems (Zabbix, Prometheus, Nagios). A critical threshold is 80-90%. - Standardize permissions. Configure your applications (web servers, databases) to create logs with group write access that
rootcan read. For example, for nginx:chown root:admandchmod 640for logs. - Annual audit of logrotate configurations. Check files in
/etc/logrotate.d/for obsolete paths or configs for uninstalled programs. - Use
copytruncatedeliberately. Apply this option only for applications that don't support log rotation signals (e.g., some Java apps). For nginx, Apache, haproxy, the standard method withpostrotate/prerotateis better. - Test changes. Any change to
logrotateconfigs or log permissions should be tested withsudo logrotate -d /etc/logrotate.confbefore the system runs them automatically.
Method 7: Debugging with a Verbose Log (for Complex Cases)
If standard methods didn't help, enable maximum verbose logging for logrotate.
- Create a file for the verbose log:
sudo touch /var/log/logrotate-debug.log sudo chmod 644 /var/log/logrotate-debug.log - Run
logrotatewith all output (stdout and stderr) redirected to this file:
Or for a real run (withsudo logrotate -d /etc/logrotate.conf 2>&1 | sudo tee /var/log/logrotate-debug.log-vfor verbosity):sudo logrotate -vf /etc/logrotate.conf 2>&1 | sudo tee -a /var/log/logrotate-debug.log - Examine the resulting
/var/log/logrotate-debug.logfile. It will show the sequence of config reading, checking each file, and the exact point of failure. This is the most reliable diagnostic method.
Important: After diagnosis, remember to delete or clear the debug log so it doesn't itself cause disk space issues.