Periodically clean up application logs

更新时间:
复制 MD 格式

Large log files can fill up container storage and cause application errors. To prevent this, configure a cron daemon in your SAE application to automatically delete old log files on a schedule.

This topic covers two deployment paths: applications deployed from a code package and applications deployed from a container image.

Prerequisites

Before you begin, ensure that you have:

  • An SAE application deployed from a code package or a container image

Note

For the list of operating system versions available in each runtime environment, see Deploy an application from a code package.

How it works

SAE uses the native cron daemon in the container's runtime environment to run scheduled cleanup tasks. You configure a crontab entry that runs find to delete log files older than a specified threshold. The cron daemon starts automatically each time the container starts, using the PostStart hook in Application Lifecycle Management.

Set up log cleanup for code package deployments

The setup steps differ depending on whether your runtime uses Debian or CentOS.

Debian

Step 1: Install the cron daemon and add a crontab entry

When creating or deploying your application, set Application Deployment Method to Code Package-based Deployment, click Configure Code Package-based Deployment, and enable Custom Runtime Environment Settings (Including Tool Pre-installation, File Download/Modification, and Runtime Dependency/Extension Installation). Enter the following script:

#!/bin/bash

set -o errexit
set -o nounset
set -o pipefail

# Install the cron daemon
apt-get update || true
apt-get install -y cron

# Add a crontab entry to delete log files not modified in the past 7 days, running hourly
echo "0 * * * * root find /home/admin/logs/* -mtime +7 -name '*.log'  -exec rm -rf {}  \;">>/etc/crontab
Note

To verify your setup quickly, replace the crontab entry above with the following line, which runs every minute and deletes files not modified in the past minute. After confirming the cleanup works, switch back to the production configuration.

echo "* * * * * root find /home/admin/logs/* -mmin +1 -name '*.log'  -exec rm -rf {}  \;">>/etc/crontab

Step 2: Start the cron daemon on container startup

In the Application Lifecycle Management section, enable PostStart Settings and enter:

service cron start

CentOS

Step 1: Add a crontab entry

On CentOS, the cron daemon is pre-installed. When creating or deploying your application, set Application Deployment Method to Code Package-based Deployment, click Configure Code Package-based Deployment, and enable Custom Runtime Environment Settings (Including Tool Pre-installation, File Download/Modification, and Runtime Dependency/Extension Installation). Enter the following script:

#!/bin/bash

# The cron daemon is pre-installed on CentOS — no installation needed.

# Add a crontab entry to delete log files not modified in the past 7 days, running hourly
echo "0 * * * * root find /home/admin/logs/* -mtime +7 -name '*.log'  -exec rm -rf {}  \;">>/etc/crontab
Note

To verify your setup quickly, replace the crontab entry above with the following line, which runs every minute and deletes files not modified in the past minute. After confirming the cleanup works, switch back to the production configuration.

echo "* * * * * root find /home/admin/logs/* -mmin +1 -name '*.log'  -exec rm -rf {}  \;">>/etc/crontab

Step 2: Start the cron daemon on container startup

In the Application Lifecycle Management section, enable PostStart Settings and enter:

/sbin/crond start

Set up log cleanup for container image deployments

  1. During the image build phase, install the cron daemon and add the crontab entry. Follow the same scripts described in the Debian or CentOS sections above, depending on the base image OS.

  2. When creating or deploying your application, enable Post-start Handler (PostStart Settings) in the Application Lifecycle Management section and enter the startup command for your OS:

    • Debian-based image: service cron start

    • CentOS-based image: /sbin/crond start

Cron expression reference

All crontab entries in this topic follow this format:

f1 f2 f3 f4 f5
FieldRepresentsExample
f1Minute0 = at minute 0
f2Hour* = every hour
f3Day of month* = every day
f4Month* = every month
f5Day of week0 = Sunday

Common schedules:

ScheduleExpression
Every minute* * * * *
Every hour0 * * * *
Every day at midnight0 0 * * *
Every week (Sunday midnight)0 0 * * 0
Every month (1st, midnight)0 0 1 * *

Find command parameters

The find command in the crontab entry targets /home/admin/logs/* and removes files matching *.log:

ParameterDescription
-mtime +7Matches files not modified in more than 7 days
-mmin +1Matches files not modified in more than 1 minute (for quick verification)
-exec rm -rf {} \;Deletes each matched file

The entry runs as the root user and appends the schedule to /etc/crontab.