Home >Backend Development >PHP Tutorial >Linux scheduled file cleaning script shell
Everything that is often used in work is collected from the Internet. Record it for easy search
Statement writing:
find corresponding directory -mtime + number of days -name "file name" -exec rm -rf {} ;
Example 1:
find /usr/local/backups -mtime +10 -name “*.*” -exec rm -rf {} ;
will find all files with “.” in the /usr/local/backups directory 10 days ago. File deletion
find: Linux search command, users search for files with specified conditions
/usr/local/backups: Any directory you want to clean up
-mtime: Standard statement writing
+10: Find files 10 days ago Files, the number here represents the number of days, +30 means searching for files 30 days ago
"*.*": the data type you want to find, "*.jpg" means searching for all files with the extension jpg, "*" means searching All files, this can be used flexibly, draw inferences from one example
-exec: Fixed writing method
rm -rf: Forced deletion of files, including directories
{};: Fixed writing method, a pair of braces + spaces++;
Example 2 :
1.#touch /usr/local/bin/clear
#chmod 777 clear
Create a new executable file clear
2.vi clear
Edit the clear file as follows:
#!/bin/sh
find /usr/local/backups -mtime +10 -name “*.*” -exec rm -rf {} ;
ok, save and exit
3.#crontab -e
Add the clear file to the system plan The task will be executed automatically when the time comes.
Input:
* 2 * * */usr/local/bin/clear
The setting here is to execute the clear file at 2 am every day for data cleaning. You can study cron and formulate your own needs Scheduled task
Example:
#!/bin/sh
find /usr/local/jboss-4.2.3.GA/server/default/log -mtime +6 -name “server.log.*” - exec rm -rf {} ;
exit
[root@web3 ~]# crontab -l
Edit the user's Crontab file
crontabl -e
The Crontab file created by the user is stored in /var/spool/cron , the file name is consistent with the user name.
The format is divided into six sections. The first five sections are the time setting sections, and the sixth section is the command section to be executed.
The format is as follows: * * * * *
The meaning of the time sections is as shown in Table 2:
Paragraph
meaning
value range
The first paragraph
represents minutes
0—59
The second paragraph
represents hours
0—23
The third paragraph
represents date
1— 31
The fourth paragraph
represents the month
1—12
The fifth paragraph
represents the day of the week, 0 represents Sunday
0—6
Name: crontab
Permissions: All users
Usage:
crontab [ -u user ] file
crontab [ -u user ] { -l | -r | -e }
Explanation:
crontab is used to allow users to execute programs at a fixed time or fixed interval. In other words , which is similar to the user's schedule. -u user refers to setting the schedule of the specified user. The premise is that you must have its permissions (for example, root) to specify other people's schedules. If -u user is not used, it means setting your own schedule.
Number of meals:
-e: Execute a text editor to set the schedule. The default text editor is VI. If you want to use another text editor, please set the VISUAL environment variable to specify which text to use. Editor (such as setenv VISUAL joe)
-r: Delete the current schedule
-l: List the current schedule
The format of the schedule is as follows:
f1 f2 f3 f4 f5 program
where f1 is represents minutes, f2 represents hours, f3 represents the day of a month, f4 represents the month, and f5 represents the day of the week. program represents the program to be executed.
When f1 is *, it means the program will be executed every minute, when f2 is *, it means the program will be executed every hour, and so on
When f1 is a-b, it means the program will be executed from minute a to minute b , when f2 is a-b, it means that it will be executed from the a-th hour to the b-th hour, and so on. When f1 is */n, it means that it will be executed every n-minute time interval. When f2 is */n, it means every n-hour time interval. Execute once, and so on. When f1 is a, b, c,..., it means that the a, b, c,... minute will be executed. When f2 is a, b, c,..., it means the a, b, c... minute. Hourly execution, and so on. Users can also store all settings in the file first, and use crontab file to set the schedule.
Example:
Execute /bin/ls at the 0th minute of every hour every day of the month:
0 7 * * * /bin/ls
In 12 months, every day from 6 am to 12 am, every 20 Execute /usr/bin/backup once every minute:
0 6-12/3 * 12 * /usr/bin/backup
Send a letter to alex@domain.name at 5:00 pm every day from Monday to Friday:
0 17 * * 1-5 mail -s “hi” alex@domain.name /dev/null 2>&1
Example (create the entire process of cron, and enter the current time in test.txt every minute):
1. Log in to the Linux system as an ordinary user (I am using CentOS4.1)
2. $crontab –e
Note: The default editor of the system is VIM. If not, please add the following shell:
$EDITOR=vi
$export EDITOR
3. Enter “*/1 * * * * date >> $HOME/test.txt”, save and exit VIM
4. $su root
5. $cd /etc/ init.d
6. ./crond restart
Let’s take a look at a few specific examples:
● 0 */2 * * * /sbin/service httpd restart means restarting apache every two hours
● 50 7 * * * /sbin/service sshd start means to start the ssh service at 7:50 every day
● 50 22 * * * /sbin/service sshd stop means to close the ssh service at 22:50 every day
● 0 0 1 ,15 * * fsck /home Check the /home disk on the 1st and 15th of every month
● 1 * * * * /home/bruce/backup Execute the /home/bruce/backup file at the first minute of every hour
● 00 03 * * 1-5 find /home “*.xxx” -mtime +4 -exec rm {} ; Every Monday to Friday at 3 o’clock, search for the file named *.xxx in the directory /home. And delete files older than 4 days.
● 30 6 */10 * * ls means that the ls command is executed once every month on the 1st, 11th, 21st, and 31st at 6:30