Centosshell General Automatic Log cleanup script

Source: Internet
Author: User
1. this article is a simplified script for automatic system deployment. The main function is to clear logs from all directories. based on the given log directory, the deletion time is combined with crontab to clean up the log. After the cleaning is complete, generate a monthly cleanup log in/var/log/deltelog/. 3. because the script uses mtime (last modification time) to delete expired backups, it can be used to delete expired backups without the limitation of file names. shell tips

1. This article is a simplified script for automatic system deployment. The main function is to clear logs from all directories.

2. Based on the given log directory, the deletion time is combined with crontab to clear logs. After cleaning is complete, the cleanup logs by month are generated in/var/log/deltelog /.

3. Expansion. Because the script uses mtime (last modification time) for deletion, it can be used to delete expired backups, and is not limited by the file name.

Shell tips

Find? Type f? Print outputs the searched files based on line breaks or spaces. Different responses are made to different sh files. If xargs is used to delete and change files, it will be affected.

So you need to add? Print0 uses null as the boundary symbol to marry xargs? O to format the input

When using find, follow the minimum result set principle. The find analytical expression is left-to-right, so that all the filter symbols on the leftmost can filter the maximum data.

You can add it to crontab as needed.

Shell script

#!/bin/sh  ###########################  #delete log blog.duplicatedcode.com  # in_day_num: like 1 2 is delete 2day ago logs  # in_log_path like tomcat log home  ###########################  in_log_path=${1}  in_day_num=${2}  tmp_delete_log=/var/log/deletelog/"`date +%Y%m`.log"  deleteLog()  {  inner_num=${1}  #find log  echo "[`date`] >> start delete logs---" >> $tmp_delete_log  find ${in_log_path} -type f -mtime ${inner_num} -print0 | xargs -0 rm -rf  echo "[`date`] >> end delete logs---" >> $tmp_delete_log  }  init()  {  mkdir -p /var/log/deletelog/  }  main()  {  init  if [ -z ${in_log_path} ];then  echo "[`date`] >> error log_path not init---" >> $tmp_delete_log  return  fi  inner_day_num=+7  if [[ -n ${in_day_num} ]] && [[ ${in_day_num} -ge 1 ]] ; then  ${inner_day_num}=${in_day_num}  fi  deleteLog ${inner_day_num}  }  main

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.