A simple method to implement Linux timed log deletion

A simple method to implement Linux timed log deletion

Introduction

Linux is a system that can automatically generate files, logs, emails, backups, etc. Although hard disks are cheap now, we can have a lot of hard disk space to waste for these files. It is very refreshing to let the system clean up some unnecessary files regularly.

Various logs will be generated in the project, and as time goes by, the number of logs will increase. Logs older than a certain time have no reference value and will take up space, so most projects will delete the logs. The requirement of this article is to delete logs older than 7 days, which is roughly divided into two steps: one is the command to delete the logs, and the other is to set a scheduled task.

Delete the logs of the specified time

First, search for all logs from 7 days ago. When using find, add the -mtime parameter, which means to search for files or directories that have been changed within the specified time, in units of 24 hours. An example is find you/path -mtime +7 -name '*.log' , as follows

The search is complete, and the next step is deletion. Based on the above statement, adding the -exec parameter can achieve deletion. The parameter means that if the return value of the find command is True, the command will be executed. So the complete statement is find you/path -mtime +7 -name '*.log' -exec rm -rf {} \; . In actual execution, delete the logs older than 16 days, otherwise the following demonstration will not work

There was only one log 16 days ago. After the execution, it is not found again, indicating that the deletion was successful.

Set up scheduled tasks

Finally, add the command to the scheduled task. For information about crontab commands and execution time settings, see the references below.
Before adding, let's take a look at the logs from 14 days ago

There is a log. Edit the scheduled task, execute crontab -e, append the log deletion command to the end */1 * * * * find you/path -mtime +14 -name '*.log' -exec rm -rf {} \; and set it to execute once every minute. Wait a minute and check the logs from 14 days ago.

You can see that after adding the scheduled task, the log has been automatically deleted without manual deletion. Of course, it is not necessary to execute it every minute. Set the time to 1 o'clock every day. The final scheduled task and log deletion commands are as follows

0 1 * * * find you/path -mtime +7 -name '*.log' -exec rm -rf {} \; 

References: find command, crontab command, linux using crontab to add scheduled tasks.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of command to view log files in Linux environment
  • How to manually scroll logs in Linux system
  • Summary of 6 Linux log viewing methods
  • How to use glog log library in Linux environment
  • Detailed introduction to logs in Linux system

<<:  Problems encountered in using MySQL

>>:  JavaScript design pattern learning proxy pattern

Recommend

A pitfall and solution of using fileReader

Table of contents A pitfall about fileReader File...

HTML5+CSS3 coding standards

The Golden Rule No matter how many people are wor...

Solution to MySQL IFNULL judgment problem

Problem: The null type data returned by mybatis d...

Ten Experiences in Presenting Chinese Web Content

<br /> Focusing on the three aspects of text...

Some understanding of absolute and relative positioning of page elements

From today on, I will regularly organize some smal...

The concrete implementation of JavaScript exclusive thinking

In the previous blog, Xiao Xiong updated the meth...

How to export and import .sql files under Linux command

This article describes how to export and import ....

Native JS music player

This article example shares the specific code of ...

Use CSS to set the width of INPUT in TD

Recently, when I was using C# to make a Web progra...

How to call the interrupted system in Linux

Preface Slow system calls refer to system calls t...

Web page HTML ordered list ol and unordered list ul

Lists for organizing data After learning so many ...

Detailed explanation of Linux text editor Vim

Vim is a powerful full-screen text editor and the...

How to implement multiple parameters in el-dropdown in ElementUI

Recently, due to the increase in buttons in the b...

Detailed description of HTML table border control

Only show the top border <table frame=above>...