当前位置: 首页 > news >正文

reprint, Use of logrotate

Log management is one of those essential sysadmin tasks that can make or break your server’s performance and disk space. Without proper log rotation, your system can quickly run out of storage, crash services, or become impossible to troubleshoot when you actually need those logs. Logrotate is Ubuntu’s built-in solution for automatically archiving, compressing, and cleaning up log files based on rules you define. This guide covers everything from basic setup to advanced configurations, common gotchas you’ll encounter, and how logrotate compares to other log management approaches.

How Logrotate Works Under the Hood

Logrotate operates through a combination of cron jobs and configuration files that define rotation policies. The main process runs daily via /etc/cron.daily/logrotate, checking all configured log files against their rotation criteria like size limits, age, or manual triggers.

The rotation process follows this sequence:

  • Check if rotation criteria are met (size, time, or forced rotation)
  • Execute pre-rotation scripts if defined
  • Stop or signal the service writing to the log (optional)
  • Move/rename the current log file
  • Create a new empty log file with proper permissions
  • Compress old log files if configured
  • Remove old log files beyond the retention limit
  • Execute post-rotation scripts
  • Restart or signal the service to resume logging

Ubuntu 24 includes logrotate 3.21.0 by default, which introduced better handling of large files and improved error reporting compared to earlier versions.

Installation and Basic Setup

Logrotate comes pre-installed on Ubuntu 24, but let’s verify and check the setup:

# Check if logrotate is installed
dpkg -l | grep logrotate# Check version
logrotate --version# Install if missing (shouldn't be needed)
sudo apt update && sudo apt install logrotate

The default configuration structure includes:

  • /etc/logrotate.conf – Main configuration file
  • /etc/logrotate.d/ – Directory for service-specific configurations
  • /var/lib/logrotate/status – Rotation status tracking
  • /etc/cron.daily/logrotate – Daily cron job

Let’s examine the default global configuration:

# View main config
cat /etc/logrotate.conf# Check existing service configs
ls -la /etc/logrotate.d/# View rotation status
sudo cat /var/lib/logrotate/status

Creating Custom Log Rotation Configurations

The most common scenario is setting up rotation for application logs. Here’s a practical example for a web application:

# Create config for custom app logs
sudo nano /etc/logrotate.d/myapp# Basic configuration
/var/log/myapp/*.log {dailyrotate 30compressdelaycompressmissingoknotifemptycreate 0644 www-data www-datapostrotatesystemctl reload nginxendscript
}

For high-traffic applications that generate large logs quickly, you might need size-based rotation:

# Size-based rotation for busy applications
/var/log/myapp/access.log {size 100Mrotate 10compressdelaycompressmissingoknotifemptycreate 0644 www-data www-datacopytruncatepostrotate/usr/bin/systemctl reload myappendscript
}

Here’s a more complex example for database logs with custom compression and retention:

# Advanced MySQL log rotation
/var/log/mysql/*.log {dailyrotate 90compresscompresscmd /usr/bin/xzcompressext .xzcompressoptions -6delaycompressmissingoknotifemptycreate 0640 mysql admsharedscriptsprerotate/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf flush-logsendscriptpostrotate# Signal MySQL to reopen log filestest -x /usr/bin/mysqladmin && \/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf flush-logsendscript
}

Configuration Options and Directives

Understanding the available directives is crucial for effective log management. Here’s a comprehensive breakdown:

DirectivePurposeExampleNotes
daily/weekly/monthly Rotation frequency daily Can also use specific times
size Rotate when file reaches size size 100M Overrides time-based rotation
rotate Number of old logs to keep rotate 30 0 means delete immediately
compress Compress rotated logs compress Uses gzip by default
copytruncate Copy and truncate original copytruncate For apps that can’t reopen logs
create Create new log file create 0644 user group Alternative to copytruncate
missingok Don’t error if log missing missingok Useful for optional logs

Real-World Use Cases and Examples

Let’s look at some practical scenarios you’ll encounter in production environments.

High-Volume Web Server Logs

# Nginx access logs for high-traffic site
/var/log/nginx/access.log {hourlyrotate 168  # Keep 1 week of hourly logssize 500Mcompressdelaycompressmissingoknotifemptycreate 0644 www-data admsharedscriptsprerotateif [ -d /etc/logrotate.d/httpd-prerotate ]; then \run-parts /etc/logrotate.d/httpd-prerotate; \fiendscriptpostrotateinvoke-rc.d nginx rotate >/dev/null 2>&1endscript
}

Application Logs with Centralized Logging

For applications that send logs to centralized systems like ELK stack, you might want faster local cleanup:

# Fast cleanup for centrally logged apps
/var/log/app/{error,access,debug}.log {dailyrotate 3  # Only keep 3 days locallysize 50Mcompressmissingoknotifemptycopytruncatepostrotate# Ensure log shipper picks up rotated filessystemctl restart filebeatendscript
}

System Service Logs

# Custom service with multiple log levels
/var/log/myservice/*.log {weeklyrotate 52compressdelaycompressmissingoknotifemptycreate 0640 myservice syslogpostrotate# Send SIGUSR1 to reopen logs/bin/kill -USR1 $(cat /run/myservice.pid 2>/dev/null) 2>/dev/null || trueendscript
}

Testing and Debugging Configurations

Before deploying rotation configs to production, always test them thoroughly:

# Test specific configuration without executing
sudo logrotate -d /etc/logrotate.d/myapp# Force rotation for testing (be careful in production)
sudo logrotate -f /etc/logrotate.d/myapp# Verbose output for troubleshooting
sudo logrotate -v /etc/logrotate.conf# Check what files would be affected
sudo logrotate -d /etc/logrotate.conf | grep "rotating pattern"

To test a configuration before the daily cron runs:

# Temporarily modify status file to force rotation
sudo cp /var/lib/logrotate/status /var/lib/logrotate/status.backup# Remove specific log entry to force rotation
sudo sed -i '/myapp/d' /var/lib/logrotate/status# Run logrotate manually
sudo logrotate -v /etc/logrotate.d/myapp# Restore status file if needed
sudo cp /var/lib/logrotate/status.backup /var/lib/logrotate/status

Common Issues and Troubleshooting

Here are the most frequent problems you’ll encounter and how to solve them:

Permission Issues

# Common error: permission denied
# Check log file ownership
ls -la /var/log/myapp/# Fix ownership if needed
sudo chown -R www-data:www-data /var/log/myapp/# Verify logrotate can read the directory
sudo -u www-data ls /var/log/myapp/

Service Not Reopening Log Files

This is probably the most common issue. Services need to be told to reopen their log files after rotation:

# Wrong approach - file handle remains open
/var/log/myapp/*.log {dailyrotate 7compress# Missing postrotate script
}# Correct approach - signal service to reopen
/var/log/myapp/*.log {dailyrotate 7compresspostrotatesystemctl reload myapp# Or send specific signal# kill -USR1 $(cat /run/myapp.pid)endscript
}

Logrotate Not Running

# Check if daily cron is working
sudo run-parts --test /etc/cron.daily# Check logrotate cron job
cat /etc/cron.daily/logrotate# Check system logs for errors
grep logrotate /var/log/syslog# Check logrotate status
sudo systemctl status cron

Configuration Syntax Errors

# Validate configuration syntax
sudo logrotate -d /etc/logrotate.conf# Common syntax issues:
# - Missing endscript after postrotate
# - Incorrect file permissions in create directive
# - Invalid size specifications (use M for MB, G for GB)
# - Duplicate directives in same config block

Performance Considerations and Optimization

Logrotate performance can impact system resources, especially with large files or many simultaneous rotations:

ScenarioFile SizeRotation TimeCPU UsageOptimization
Small logs (<10MB) 1-10MB <1 second Low Standard compression
Medium logs (10-100MB) 10-100MB 5-30 seconds Medium delaycompress
Large logs (100MB-1GB) 100MB-1GB 1-10 minutes High copytruncate + nice
Very large logs (>1GB) >1GB 10+ minutes Very High Size-based rotation

For large files, consider using copytruncate to minimize service interruption:

# Optimized config for large files
/var/log/bigapp/massive.log {size 500M  # Rotate before files get too largerotate 20compressdelaycompresscopytruncate  # Avoids service restartmissingoknotifempty# Use nice to reduce CPU priorityprerotateecho "Starting rotation of large log file"endscriptpostrotate# Optional: signal external monitoringecho "Large log rotated" | wallendscript
}

Advanced Configuration Techniques

Custom Compression Settings

# Use different compression algorithms
/var/log/archive/*.log {dailyrotate 365compresscompresscmd /usr/bin/xzcompressext .xzcompressoptions -9  # Maximum compressionuncompresscmd /usr/bin/unxzdelaycompressmissingoknotifempty
}

Conditional Rotation Based on Time

# Rotate only during off-peak hours
/var/log/peak-app/*.log {dailyrotate 30compressmissingoknotifemptyprerotate# Only rotate between 2-4 AMhour=$(date +%H)if [ $hour -lt 2 ] || [ $hour -gt 4 ]; thenexit 1fiendscriptpostrotatesystemctl reload peak-appendscript
}

Log Archival to Remote Storage

# Archive old logs to S3 or remote storage
/var/log/important/*.log {dailyrotate 7  # Keep only 1 week locallycompressdelaycompressmissingoknotifemptycreate 0644 app applastaction# Archive logs older than 7 days to remote storagefind /var/log/important -name "*.log.*.gz" -mtime +7 -exec \aws s3 cp {} s3://my-log-bucket/$(hostname)/ \; -deleteendscript
}

Comparison with Alternative Log Management Solutions

SolutionBest ForProsConsResource Usage
logrotate System logs, simple rotation Built-in, reliable, lightweight Limited features, cron-dependent Very Low
rsyslog Centralized logging, filtering Real-time, powerful filtering Complex configuration Medium
journald systemd services Integrated with systemd, binary format Not universal, requires systemd Medium
Fluentd/Fluent Bit Container environments, streaming Real-time, many outputs Resource intensive, complex High

Best Practices and Security Considerations

Follow these practices to maintain secure and efficient log rotation:

  • Always use the create directive with appropriate permissions rather than relying on defaults
  • Set up log retention policies that comply with your organization’s requirements
  • Use delaycompress to allow log analysis tools to finish processing files
  • Monitor logrotate execution through system logs
  • Test configurations in staging environments before production deployment
  • Consider disk space carefully when setting rotation policies
  • Use sharedscripts when multiple log files share the same service
  • Implement proper error handling in pre/post rotation scripts

Security Configuration Example

# Secure log rotation with proper permissions
/var/log/secure/*.log {weeklyrotate 52compressdelaycompressmissingoknotifemptycreate 0600 root root  # Restrict access to sensitive logssharedscriptsprerotate# Ensure only root can read rotated security logsumask 077endscriptpostrotate# Log rotation event for audit purposeslogger "Security logs rotated by logrotate"systemctl reload rsyslogendscript
}

Monitoring and Maintenance

Set up monitoring to ensure logrotate continues working properly:

# Create monitoring script
sudo nano /usr/local/bin/check-logrotate.sh#!/bin/bash
# Check if logrotate ran successfully today
last_run=$(stat -c %Y /var/lib/logrotate/status)
current_time=$(date +%s)
hours_since=$((($current_time - $last_run) / 3600))if [ $hours_since -gt 25 ]; thenecho "WARNING: logrotate hasn't run in $hours_since hours"exit 1
fi# Check for errors in syslog
if grep -q "logrotate.*error" /var/log/syslog; thenecho "ERROR: logrotate errors found in syslog"exit 1
fiecho "logrotate is working properly"
exit 0

For environments with VPS hosting or dedicated servers, consider setting up centralized log monitoring to track rotation status across multiple systems.

Regular maintenance tasks include:

  • Reviewing disk usage trends to adjust retention policies
  • Checking for failed rotations in system logs
  • Updating configurations when adding new services
  • Testing rotation configurations after system upgrades
  • Monitoring compression ratios to optimize storage

With proper logrotate configuration, you can automate log management effectively while maintaining system performance and ensuring you have the log data you need for troubleshooting and compliance requirements.

http://www.jsqmd.com/news/155809/

相关文章:

  • AI开发者必备工具链:PyTorch + Jupyter + CUDA一体化镜像
  • 推荐阅读:深入理解Socket网络编程及其在现代通信中的作用
  • linux-vim常规操作
  • 99线怎么算?99线、90线盯哪根? 面试官:你连这都分不清,出门左拐不送。直接凉凉
  • C++虚函数表与多重继承内存布局深度剖析
  • YOLO推理请求限速控制:保护GPU服务稳定性
  • 一篇爆款技术文带来的流量:如何引导用户购买GPU算力
  • 模拟信号处理发展,从硬件优化到智能生态的全维度突破
  • YOLO系列再进化:YOLOv11适配PyTorch-CUDA全流程
  • PyTorch-CUDA基础镜像使用指南:支持多卡并行的AI训练环境
  • 2025最新!10个AI论文软件测评:本科生写论文救星大公开
  • 解锁2026年商业未来:四大核心概念深度解析
  • 机器学习——基本概念
  • 计算机毕设java后疫情时代小区服务网站 基于Java的后疫情时代社区服务管理系统设计与实现 Java技术驱动的后疫情时代小区服务平台开发
  • PyTorch+CUDA环境搭建耗时太久?试试我们的镜像方案
  • 机器学习——生态系统
  • 从爱好到专业:AI初学者如何跨越CAIE认证的理想与现实鸿沟
  • 静态库与动态库
  • 卷积神经网络性能瓶颈突破:使用CUDA加速卷积运算
  • 那台NAS,是我为自己买的“赛博菩萨”
  • PyTorch-CUDA镜像安全性评估:企业级部署注意事项
  • SSH连接超时?PyTorch-CUDA镜像中的安全设置建议
  • 2025.9.18社团管理(二)
  • YOLOv9 vs YOLOv10:谁更适合你的工业视觉应用场景?
  • PyTorch-CUDA-v2.6镜像发布:专为大模型训练优化的GPU环境
  • Conda环境迁移难题破解:容器化PyTorch解决方案
  • 编写并使用dll
  • YOLOv10新增PGI辅助监督,梯度传播更充分
  • YOLO训练数据合成技术:用GAN生成更多样本
  • 专科生必看!8个高效降AIGC工具推荐,轻松应对AI检测