ptero-eggs icon indicating copy to clipboard operation
ptero-eggs copied to clipboard

Implement automatic log rotation system to prevent indefinite log growth

Open Copilot opened this issue 5 months ago • 0 comments

Problem

Pterodactyl containers running the webhost egg suffer from logs growing indefinitely without any rotation mechanism. Since Alpine Linux containers don't include logrotate by default, log files can consume all available disk space over time, requiring manual intervention to delete logs.

The main log files affected include:

  • Nginx access logs (/home/container/logs/access.log)
  • Nginx error logs (/home/container/logs/error.log)
  • PHP-FPM logs (/home/container/php-fpm.log)
  • Optional additional logs when enabled in nginx configuration

Solution

This PR implements a comprehensive, dependency-free log rotation system that automatically manages log files without requiring logrotate or any external tools.

Key Features

Automatic Log Rotation Daemon

  • Background process that continuously monitors log file sizes
  • Automatically rotates logs when they exceed configurable size limits
  • Starts automatically with the server, requiring no manual intervention

Safe Service-Aware Rotation

  • Sends USR1 signals to nginx and PHP-FPM processes to safely reopen log files
  • Ensures zero-downtime log rotation without service interruption
  • Handles missing processes gracefully without errors

Configurable Retention Policy

  • LOG_MAX_SIZE: Maximum log file size before rotation (default: 10M)
  • LOG_KEEP_COUNT: Number of rotated log files to retain (default: 5, range: 1-20)
  • LOG_CHECK_INTERVAL: How often to check log sizes in seconds (default: 300, range: 60-3600)

User-Friendly Management Tools

  • scripts/logrotate.sh: Core rotation engine with status, rotate, and daemon modes
  • scripts/logmanager.sh: Simplified interface for common operations
  • Both scripts provide comprehensive help and usage examples

Implementation Details

Shell Compatibility

  • Uses POSIX shell (ash) for full Alpine Linux compatibility
  • Avoids bash-specific features like arrays and local keyword
  • Robust error handling for edge cases and missing files

Automated Integration

  • Log rotation daemon starts automatically in start.sh
  • Installation script updated to include new scripts directory
  • New environment variables added to egg configuration with proper validation

Documentation

  • Comprehensive README updates with usage examples
  • Detailed technical documentation in docs/LOG_MANAGEMENT.md
  • Troubleshooting guide and configuration examples

Usage Examples

Basic Status Check

./scripts/logmanager.sh status

Manual Rotation

./scripts/logmanager.sh rotate

Custom Configuration for High-Traffic Server

# Set in Pterodactyl panel environment variables
LOG_MAX_SIZE=100M
LOG_KEEP_COUNT=10
LOG_CHECK_INTERVAL=180

Benefits

  • Eliminates manual log deletion - Fully automated system handles everything
  • Prevents disk space issues - Configurable limits protect against runaway logs
  • Zero maintenance overhead - Set once, runs automatically forever
  • Flexible configuration - Adapts to different server needs and traffic patterns
  • Production ready - Comprehensive error handling and edge case management

This solution completely addresses the original issue by providing an automated alternative to manual log deletion, ensuring Pterodactyl webhost servers can run indefinitely without log-related disk space problems.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Sep 17 '25 10:09 Copilot