In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Introduction
Linux is a system that automatically generates files, such as logs, emails, backups, etc. Although the hard drive is cheap now, we can have a lot of hard disk space for these files to waste, so it is a pleasant thing for the system to clean up some unneeded files regularly.
A variety of logs are generated in the project, and more and more logs are generated over time. Logs that exceed a certain period of time will have no reference value and will take up space, so general projects will delete logs. The requirement of this article is to delete the log before 7 days, which is roughly divided into two steps: one is the command to delete the log, and the other is to set up scheduled tasks.
Delete the log at a specified time
The first is to find all logs from 7 days ago, using find to find the-mtime parameter, which means to find files or directories that have been changed at a specified time, calculated in 24 hours. The example is find you/path-mtime + 7-name'* .log', as follows
The search is complete, and the next step is deletion. On the basis of the above statement, the deletion can be achieved by adding the-exec parameter, which means that the find instruction is executed assuming that the return value of the instruction is True. So the complete statement is find you/path-mtime + 7-name'* .log'- exec rm-rf {}\;. In practice, delete the log from 16 days ago, otherwise there is no way to demonstrate it below.
There was only one log 16 days ago, and the search does not exist again after execution, indicating that the deletion was successful.
Set up scheduled tasks
Finally, add the command to the scheduled task. For information about the crontab command and the setting of the execution time, you can see the Resources below.
Before you add it, let's take a look at the log from 14 days ago.
There is a journal. Edit the scheduled task and execute crontab-e to append the delete log command to the end * / 1 * find you/path-mtime + 14-name'* .log'- exec rm-rf {}\;, which is set to be executed every minute. Wait a minute, and then check the log from 14 days ago.
You can see that after adding scheduled tasks, there is no manual deletion, the log has been automatically deleted. Of course, you don't need to execute it every minute, set the time to 1 o'clock every day, and the final timing task and delete log command are as follows
0 1 * find you/path-mtime + 7-name'* .log'- exec rm-rf {}\
Resources: find commands, crontab commands, linux use crontab to add scheduled tasks.
Summary
The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.