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
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/crontabTo 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/crontabStep 2: Start the cron daemon on container startup
In the Application Lifecycle Management section, enable PostStart Settings and enter:
service cron startCentOS
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/crontabTo 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/crontabStep 2: Start the cron daemon on container startup
In the Application Lifecycle Management section, enable PostStart Settings and enter:
/sbin/crond startSet up log cleanup for container image deployments
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.
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 startCentOS-based image:
/sbin/crond start
Cron expression reference
All crontab entries in this topic follow this format:
f1 f2 f3 f4 f5| Field | Represents | Example |
|---|---|---|
f1 | Minute | 0 = at minute 0 |
f2 | Hour | * = every hour |
f3 | Day of month | * = every day |
f4 | Month | * = every month |
f5 | Day of week | 0 = Sunday |
Common schedules:
| Schedule | Expression |
|---|---|
| Every minute | * * * * * |
| Every hour | 0 * * * * |
| Every day at midnight | 0 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:
| Parameter | Description |
|---|---|
-mtime +7 | Matches files not modified in more than 7 days |
-mmin +1 | Matches 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.