logrotate未每日轮转日志文件求助及配置详情
Let's walk through why your daily log rotation isn't triggering, using your provided configuration as a starting point.
Your Current Setup
First, here's the custom config you placed in /etc/logrotate.d/:
/opt/appliedpathways/tomcat/instance_2/logs/*.log /opt/appliedpathways/tomcat/instance_2/logs/catalina.out { daily copytruncate missingok ifempty rotate 30 maxage 30 compress dateext dateformat -%Y%m%d-%s }
And the relevant snippet from /etc/logrotate.conf:
# see "man logrotate" for details # rotate log files weekly weekly # keep 4 weeks worth of backlogs rotate 4 # create new (empty) log files after rotating old ones create
Step-by-Step Troubleshooting
1. Verify Logrotate's Cron Schedule
Logrotate relies on system cron jobs to run daily. First, check if the daily cron script exists and is executable:
ls -l /etc/cron.daily/logrotate
You should see a file with execute permissions (e.g., -rwxr-xr-x). If it's missing, reinstall logrotate to restore this script.
Also confirm the cron service is running:
systemctl status cron # Use `systemctl status crond` on RHEL/CentOS-based distros
2. Test the Configuration Manually
Run logrotate in debug mode to catch syntax errors or path issues without modifying any files:
logrotate -d /etc/logrotate.d/your-config-filename
Replace your-config-filename with the actual name of your config file in /etc/logrotate.d/. Keep an eye out for lines starting with error: —these will point to problems like missing log files or permission denials.
For verbose output (to see exactly what logrotate would do during a real run), use:
logrotate -v /etc/logrotate.d/your-config-filename
3. Check for Configuration Conflicts
Your custom config uses daily, which correctly overrides the global weekly setting in logrotate.conf. A few quick checks here:
ifempty: This tells logrotate to rotate even empty logs (default behavior), which is fine—but if you intended to skip empty logs, swap it fornotifempty.dateformat: The-%Y%m%d-%sformat is valid, but double-check for typos (like extra hyphens) that might cause unexpected behavior.
4. Validate File Permissions
Logrotate runs as root by default, but confirm root has read/write access to your Tomcat log directory and files:
ls -ld /opt/appliedpathways/tomcat/instance_2/logs/ ls -l /opt/appliedpathways/tomcat/instance_2/logs/*.log /opt/appliedpathways/tomcat/instance_2/logs/catalina.out
If logs are owned by a non-root user (e.g., tomcat), add su tomcat tomcat to your logrotate config block to run rotation as the Tomcat user instead.
5. Check Logrotate's Activity Logs
Logrotate logs its actions to /var/log/logrotate.log (location may vary slightly by distro). Filter for entries related to your Tomcat logs:
grep instance_2 /var/log/logrotate.log
This will show if rotation was attempted, skipped, or failed—and exactly why.
Quick Test: Force Immediate Rotation
If debug mode shows no errors, you can force an immediate rotation to validate your config:
logrotate -f /etc/logrotate.d/your-config-filename
Check your Tomcat log directory afterward to see if rotated/compressed files appear as expected.
内容的提问来源于stack exchange,提问作者marocano1

