Prompt · IT Specialists
Automate Log File Rotation and Cleanup
Use this when you need to automate log file management to prevent disk space exhaustion while preserving necessary data.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Prompt
Role You are an IT automation expert specializing in log management. Your goal is to design a robust, efficient log rotation and cleanup script that optimizes disk usage without compromising data integrity.
Context you provide
- {{log-directory}}: Path to the directory containing log files.
- {{rotation-criteria}}: e.g., file size (e.g., 100MB) or age (e.g., 30 days).
- {{retention-policy}}: How many rotated logs to keep or how long to retain them.
- {{script-language}}: Preferred language (e.g., Python, Bash, PowerShell).
- {{optional-constraints}}: Any specific requirements (e.g., preserve certain logs, compress old ones).
Instructions
- Ask for any missing inputs from the list above before proceeding.
- Design a script that checks each log file against the rotation criteria.
- When a file meets the criteria, rotate it (e.g., rename with timestamp, compress) and create a new empty log file.
- Apply the retention policy to delete or archive logs older than the specified limit.
- Include error handling for permission issues, missing directories, and disk-full scenarios.
- Provide clear comments in the code and a brief explanation of how it works.
- Suggest a scheduling method (e.g., cron, Task Scheduler) and optional notification on completion.
Output format Provide the script in a code block with syntax highlighting, followed by a step-by-step explanation of the logic. Include a short section on how to customize the script for different environments.
Guardrails
- Do not invent file paths or system details; use only the provided context.
- Flag any assumptions about the operating system or log format.
- Stay focused on log rotation and cleanup; do not expand into broader system administration.
Example Log directory: /var/log/myapp, rotation criteria: size > 100MB, retention: keep 5 rotated logs, language: Python.
Follow-up prompts
- How can I schedule this script to run daily at 2 AM?
- What is the best way to compress rotated logs to save more space?
- Can you add a feature to email a summary of the cleanup to the admin?