You must periodically clean up application logs. Otherwise, large log files can consume storage space and cause application errors. Although you can implement log cleanup in your application code, this topic describes an alternative solution. You can use a simple configuration to periodically clean up logs for Serverless App Engine (SAE) applications.
Overview
This solution uses the Cron tool in the application runtime environment to automatically run log cleanup tasks based on your scheduled task configuration.
Procedure
Applications deployed from a code package
The following sections describe how to install and configure the Cron tool for different operating system versions.
For more information about the operating system versions that correspond to application runtime environments, see Deploy an application from a code package.
Debian operating system
Install the Cron tool and configure a scheduled task
To create or deploy an 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). Then, enter the following command:
#!/bin/bash set -o errexit set -o nounset set -o pipefail # Install the Cron tool: apt-get update || true apt-get install -y cron # Configure the scheduled task (for more information, see the "Scheduled task parameters" section of this topic): echo "0 * * * * root find /home/admin/logs/* -mtime +7 -name '*.log' -exec rm -rf {} \;">>/etc/crontab # Runs every hour to delete log files that have not been modified for more than 7 days. # To quickly verify the result, you can use the following configuration: # echo "* * * * * root find /home/admin/logs/* -mmin +1 -name '*.log' -exec rm -rf {} \;">>/etc/crontab # Runs every minute to delete log files that have not been modified for more than 1 minute.Start the Cron tool
When you create or deploy an application, enable PostStart Settings in the Application Lifecycle Management area and enter the following command:
service cron start
CentOS operating system
Install the Cron tool and configure a scheduled task
When you create or deploy an application, set Application Deployment Method to Code Package-based Deployment, click Configure Code Package-based Deployment, enable Custom Runtime Environment Settings (Including Tool Pre-installation, File Download/Modification, and Runtime Dependency/Extension Installation), and enter the following command:
#!/bin/bash # The Cron tool is included in the runtime environment and does not need to be installed. # Configure the scheduled task (for more information, see the "Scheduled task parameters" section of this topic): echo "0 * * * * root find /home/admin/logs/* -mtime +7 -name '*.log' -exec rm -rf {} \;">>/etc/crontab # Runs every hour to delete log files that have not been modified for more than 7 days. # To quickly verify the result, you can use the following configuration: # echo "* * * * * root find /home/admin/logs/* -mmin +1 -name '*.log' -exec rm -rf {} \;">>/etc/crontab # Runs every minute to delete log files that have not been modified for more than 1 minute.Start the Cron tool
When creating or deploying an application, in the Application Lifecycle Management section, enable PostStart Settings and enter the following command:
/sbin/crond start
Applications deployed from an image
During the image build phase, install the Cron tool and configure the scheduled task.
When you create or deploy an application, enable Post-start Handler (PostStart Settings) in the Application Lifecycle Management section and enter one of the following commands:
For images based on the Debian operating system:
service cron startFor images based on the CentOS operating system:
/sbin/crond start
Scheduled task parameters
The following section describes the command for configuring a scheduled task in detail:
echo "0 * * * * root find /home/admin/logs/* -mtime +7 -name '*.log' -exec rm -rf {} \;">>/etc/crontab/etc/crontab: The configuration file for scheduled tasks.0 * * * *: The Crontab expression. This expression specifies the running time for the scheduled task. You can customize the expression. The format is as follows:f1 f2 f3 f4 f5In this format,
f1represents the minute,f2represents the hour,f3represents the day of the month,f4represents the month, andf5represents the day of the week.When
f1is*, the program is executed every minute. Whenf2is*, the program is executed every hour, and so on.Common Crontab expressions are as follows:
Running time
Format
Run once every minute
* * * * *
Run once every hour
0 * * * *
Run once every day
0 0 * * *
Run once every week
0 0 * * 0
Run once every month
0 0 1 * *
root: The user that runs the scheduled task.find /home/admin/logs/* -mtime +7 -name '*.log' -exec rm -rf {} \;: This command finds all log files in the /home/admin/logs folder and deletes files that have not been modified for more than 7 days.-mtimespecifies the time in days, and-mminspecifies the time in minutes.