Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of regular automatic deletion of files by crontab+shell script under Centos7

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to solve the problem of regular automatic deletion of files in crontab+shell scripts under Centos7". In daily operation, I believe that many people have doubts about how to solve the problem of automatically deleting files on a regular basis in crontab+shell scripts under Centos7. The editor has consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the problem of "how to solve the problem of regularly deleting files automatically in crontab+shell scripts under Centos7". Next, please follow the editor to study!

Problem description:

Recently, there is a requirement that rsync synchronizes a large amount of data each time, but the database bak files need to be retained.

Keeping it for 7 days is enough, so you need to clean up the bak files in the folder automatically.

Solution:

Use shell scripts to periodically delete tasks in a folder

1. Create a shell file

[root@zabbix script] # vim backupdatesqlbread clean.shandard cards bind to find / data1/backup/kdkda\ $agkdpaykt/xnaksd/fxuij-mtime + 10-name "* .bak"-exec rm-rf {}\

Parameter description:

/ data1/backup/kdkda\ $agkdpaykt/xnaksd/fxuij # this is the path to the file path

-mtime + 10 # this is the number of days retained, and 10 is 10 days.

-name "* .bak" # this is the name of the file to be deleted, and the suffix here is to delete this type of file.

The rest are orders from linux.

2. Set shell file permissions

[root@zabbix script] # chown 777 backup_sql_clean.sh

3. Set crontab cycle execution

The crontab command is used to set instructions that are executed periodically

Description of crontab related commands:

[root@zabbix /] # crontab-e0 0 * * 7 / data/script/backup_sql_clean.sh

4. Start the crond process

The concept of crond and crontab are inseparable. Crontab is a command, commonly found in unix and unix-like operating systems, that sets instructions that are executed periodically.

This command reads instructions from a standard input device and stores them in a "crontab" file for later reading and execution. And crond is its daemon.

[root@zabbix /] # systemctl status crond.service # View crond status [root@zabbix /] # systemctl start crond.service # start crond service [root@zabbix /] # systemctl restart crond.service # restart crond service

Supplement

Here's how to delete files regularly by centos7.

1. Enter the linux system

2. Create a file with the sh suffix in any directory, such as:

3. Edit and open the file, as shown in the figure:

4. Press the "I" key or the "insert" key on the keyboard to enter edit mode

Enter:

#! / bin/shfind / data/iqmkj/backup/mysql-mtime + 7-name "* .sql"-exec rm-rf {}\

As shown in the figure:

Description:

"/ data/iqmkj/backup/mysql": the directory where the file to be deleted is located.

"+ 7": the number of days the file will be retained, that is, the file from a few days ago will be deleted.

"* .sql": deletes files with the suffix .sql.

Others are fixed characters.

5: save the edited file

Press the "esc" key on the keyboard and enter ": wq" in the current window. The file is saved successfully and automatically returned to the main interface.

6. Authorize the document

Enter:

Chown 777 backup_mysql.sh

If the authorization is not successful, directly select the file right-click to set permissions in all check or set "777".

7: create a timer using centos7's scheduled tasks

Enter "crontab-e" in the main interface to enter the scheduled task editing interface.

8. Set up scheduled tasks

Insert "0 4 * / data/iqmkj/backup/mysql/backup_mysql_clean.sh".

Description:

The file path is the file path created in step 2.

9. Save the timed task configuration

Press the "esc" key on the keyboard and enter ": wq" in the current window. The file is saved successfully and automatically returned to the main interface.

10. Start the timer

Enter "/ bin/systemctl start crond.service" in the main interface. At this point, all the configuration is complete.

Description:

Start scheduled tasks: / bin/systemctl start crond.service

Stop scheduled tasks: / bin/systemctl stop crond.service

Restart the timing task: / bin/systemctl restart crond.service

Check the status of scheduled tasks: / bin/systemctl status crond.service

At this point, the study on "how to solve the problem of regular automatic deletion of files by crontab+shell scripts under Centos7" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report