After you deploy a service to SAE, you must manage application logs. The method for cleaning up logs varies depending on the application's technology stack. For example, Java applications can use log rotation for automatic cleanup. However, applications built with other languages, such as PHP, may lack frameworks that support log rotation. If your application cannot use log rotation, you need a different solution to automatically clean up logs. This topic uses the CentOS and Debian operating systems as examples to demonstrate how to configure automatic log cleanup in the SAE console.
Solution
In the SAE console, you can use the Custom Runtime Environment Settings to install and configure Crontab for periodic log cleanup. You can set the parameters for Custom Runtime Environment Settings when you create or deploy an application. The following figure shows an example of a PHP application deployed from a ZIP package.
Procedure
Clean up files on CentOS based on last modification time
The following example runs once per hour to delete files that end with .log and were last modified more than 7 days ago.
Install Crontab.
#!/bin/bash set -o errexit set -o nounset set -o pipefail yum install -y crontabAdd the scheduled task expression and command.
echo "0 * * * * root find /home/admin/logs/* -mtime +7 -name '*.log' -exec rm -rf {} \;">>/etc/crontabImportantThe content written to the /etc/crontab file must include a username. In this example, the username is root.
Start Crontab.
Expand the Application Lifecycle Management Settings section. On the Post-start Handler (PostStart Settings) tab, enter the command to start Crontab.
/sbin/crond start
Clean up files on Debian based on last modification time
The following example runs once per hour to delete files that end with .log and were last modified more than 7 days ago.
Install Crontab.
#!/bin/bash set -o errexit set -o nounset set -o pipefail apt-get update apt-get install -y cronAdd the scheduled task expression and command.
echo "0 * * * * root find /home/admin/logs/* -mtime +7 -name '*.log' -exec rm -rf {} \;">>/etc/crontabImportantThe content written to the /etc/crontab file must include a username. In this example, the username is root.
Start Crontab.
Expand the Application Lifecycle Management Settings section. On the Post-start Handler (PostStart Settings) tab, enter the command to start Crontab.
service cron start
Crontab parameters
In both the Clean up files on CentOS based on their last modification time and Clean up files on Debian based on their last modification time procedures, the echo command is used in the custom runtime configuration to append the Crontab instruction to the /etc/crontab file. The following command is used as an example to describe the instructions in the configuration.
0 * * * * root find /home/admin/logs/* -mtime +7 -name '*.log' -exec rm -rf {} \;0 * * * *: The expression for the Crontab running time. For more information, see Crontab expression format.root: The user account that runs the command.The rest of the command is the instruction to be executed. This instruction finds all 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.
Clear file content
In scenarios where log file rotation is not used, you can clear the file content directly. For both CentOS and Debian systems, the method to install and configure Crontab is the same as described in the previous sections. The following example shows only the Crontab command.
yes | cp /dev/null /home/admin/logs/demo.logYou can change the absolute path /home/admin/logs/demo.log as needed.
Crontab expression format
The format is as follows:
f1 f2 f3 f4 f5 programIn this format, f1 represents the minute, f2 represents the hour, f3 represents the day of the month, f4 represents the month, f5 represents the day of the week, and program represents the program to execute.
When f1 is *, the program is executed every minute. When f2 is *, 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 * * |
Additional information for images
If you deploy an application using a custom image and want to use custom runtime settings to clean up logs, you must install and start Crontab in the image. You must also replace the image source with an Alibaba Cloud internal mirror source. For more information, see CentOS Mirrors.