How to regularly clean up the .out file output by application logs in Linux

Use crontab scheduled tasks to regularly clean up the .out files output by Java applications. Here are the specific steps:

  1. Write a cleanup script

First, you need to write a script to clean up the .out files in the specified directory. The following commands can be used:

#!/bin/bash
find /opt/apps/service_bff-employee-ep/log -name "*.out" -type f -exec truncate -s 0 {} \;

If there are multiple execution commands, use ; to distinguish them.

2. Save the script

Save the script to an executable file, such as /home/user/clean_logs.sh, and then use the chmod command to add executable permissions:

chmod +x /home/user/clean_logs.sh

3. Add scheduled tasks

Open the crontab editor:

crontab -e

Add the following line in the editor to execute the cleanup script every day at 1 am:

0 1 * * * /home/user/clean_logs.sh

This command will execute the /home/user/clean_logs.sh script at 1 am every day. You can modify the time setting as needed, for example, change it to once a week.

Save and exit the editor, and this scheduled task will automatically take effect.

View the crontab real-time log execution command:

tail -f /var/log/cron

Guess you like

Origin blog.csdn.net/qq_33767353/article/details/130631576