In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to solve the problem that crontab+shell scripts under Centos7 delete files automatically on a regular basis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
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
Crontab related command description: https://www.jb51.net/article/151069.htm
[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
Thank you for reading! This is the end of this article on "how to solve the problem of regular automatic deletion of files in crontab+shell scripts under Centos7". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.