Shell script automatically cleans up server logs, pictures, and other information

Source: Internet
Author: User

When doing performance testing, Linux server often generates a lot of data, such as log information, picture information, file information, and so on, after a period of time, causing the server disk space is full and crash, daily manual cleanup more trouble,

Use shell scripts to automatically clean up the script as follows

1. Delete File command:

Find corresponding directory-mtime + days-name "file name"-exec rm-rf {} \;

Instance command:

Find/home/weblogic/rc-server-tomcat-8081/logs-mtime +30-name "*.txt"-exec rm-rf {} \;

Description

Remove all files with ". txt" in the/home/weblogic/rc-server-tomcat-8081/logs directory 30 days ago. The specific parameters are described as follows:

Find:linux the search command, the user finds the file of the specified condition;

/home/weblogic/rc-server-tomcat-8081/logs: Any directory for which you want to clean up;

-mtime: standard sentence notation;

+30: Find the file 30 days ago, here the number of days to represent;

"*.log": the type of data you want to find, "*.jpg" means to find all files with the extension jpg, "*" means to find all the files, this can be used flexibly, extrapolate;

(Purchase a single interface performance test will produce a large number of purchase order pictures, and keep in the corresponding directory, resulting in the disk is often full, automatically clear the day before the purchase order picture, script, clear format changed to. jpg on the line)

-exec: fixed notation;

RM-RF: Forced deletion of files, including directories;

{} \; : fixed notation, pair of curly braces + space +\+;

2. Scheduled Tasks:

If it is too troublesome to manually execute the statement every time, you can write this small statement into an executable shell script file, and then set cron Schedule execution, then you can let the system automatically clean up the relevant files.

2.1 Creating the Shell:

touch/home/weblogic/luojie/script/auto-del-30-days-txt.sh

chmod 777 auto-del-30-days-txt.sh

Create a new executable file, auto-del-30-days-txt.sh, and assign runnable permissions

2.2 Edit Shell script:

VI auto-del-30-days-txt.sh

Edit the auto-del-30-days-txt.sh file as follows:

#!/bin/sh
Find/home/weblogic/rc-server-tomcat-8081/logs-mtime +30-name "*.txt"-exec rm-rf {} \;

OK, save exit (: wq!).

2.3 Scheduled Tasks:

#crontab-E

Add the auto-del-30-days-txt.sh execution script to the system scheduled task, to the point of automatic execution

Input:

0 * * */home/weblogic/luojie/script/auto-del-30-days-txt.sh >/dev/null 2>&1

The setting here is to execute the auto-del-30-days-txt.sh file for the Data Cleanup task 0:10 A.M. every day.

Shell script automatically cleans up server logs, pictures, and other information

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.