Linux-- timed to clear the log contents and delete log files

 

Foreword

In recent pressure to do performance testing, it will generate a lot of logs, resulting in more pressure follow-up is slow, the problem eventually fill up disk space and the like. Always to manually delete the log files, in order to avoid such problems, write a Linux log regularly clean up the script, once and for all.

 

 

1, shell scripts

  • Create a script and empowerment
touch /home/mppay/auto-truncate-log.sh
chmod u+x auto-truncate-log.sh

 

  • Edit Script
we auto-truncate-log.sh

Empty the contents of the log file [] script as follows:

#! / bin / SH 
# can fill a plurality of paths 
Workdir = ( " /home/mppay/apache-tomcat-7.0.73/logs "  " / Home / mppay / logs " )
 for WDIR in $ {Workdir [@]} ; do 
  echo -e " filepath WDIR iS $ {} " 
  # .log log files and .txt files contain markers, as well as catalina. OUT file
   the Find $ WDIR -regex "^ * \ (\ log \ | log *... \ .txt \ | catalina.out \) $ "The -type f -print -exec TRUNCATE -s {0} \;
   IF [? $ -eq 0 ]; the then 
    echo -e` date` " ! TRUNCATE logs successfully \ the n- "
  else
    echo -e `date`" truncate logs failed! \n"
  fi
done

 [] Delete the log files script is as follows:

#! / bin / SH 
# can fill a plurality of paths 
Workdir = ( " /home/mppay/apache-tomcat-7.0.73/logs "  " / Home / mppay / logs " )
 for WDIR in $ {Workdir [@]} ; do 
  echo -e " filepath WDIR iS $ {} " 
  # .log log files and .txt files contain markers, as well as catalina. OUT file
   the Find $ WDIR -regex "^ * \ (\ log \ | log *... \ .txt \ | catalina.out \) $ "The -type f -exec the -and -mtime +5 RM -rf {} \; IF [$ -eq? 0 ]; the then 
    echo -e` date` " the Delete logs successfully ! \ the n- "
  else
    echo -e `date`" delete logs failed! \n"
  fi
done

 

  • Command Description:
find path -name "filename" -type f -print -exec truncate -s 0 {} \; # Clear file contents
find path -mtime + F -name days -type " filename " -exec RM -rf {}; # delete files
the Find : Find command Linux users find the file specified criteria
-regex : This parameter indicates the input later use regular expressions to write. If it is -the symbol name is later used to write a string general, wildcards can be used at this time, but the canonical correlation will be retained.
     shell regular:
^a regular start of the string, $ a regular end of the string, a number of other non-alphabetic symbols and regular use need to be escaped; means match any character; so the file path that appears needed. escaped.
the -and : indicates again using the same command parameters, so at -the mtime;
-mtime : indication modify time attribute, behind +7is satisfied by more than 7 days, i.e., modified in 7 days or more files or folders; and -7represents less than seven days to meet,7represents just seven days;
- of the type : a lookup file attributes, followed by f indicate to find files, and d represents the search for a folder;
- Print : print out the file conforms;
-exec : It indicates that the following command to be executed in front of the back of the matching file or folder. Note that the command requires a pair of rear child {}, a space and a last end of a semicolon;

 

 

2, crontab scheduled tasks

The auto-truncate-log.sh execute the script added to the system scheduled tasks, scheduled automatic execution:

crontab -e

Input:

* */1 * * * /home/mppay/auto-truncate-log.sh >> /home/mppay/auto-truncate-log.log

Set here every hour to perform cleanup tasks auto-truncate-log.sh log file, specify the log output.

Results of the:

Guess you like

Origin www.cnblogs.com/caoweixiong/p/12165915.html