Linux automatic cleanup log script sharing _linux Shell

Source: Internet
Author: User

1. A streamlined script for automating the deployment of systems, with the main function of cleaning up all directory logs

2. According to the given log directory, delete time combined with crontab to clean the log, clean up after completion, after the/var/log/deltelog/generated in the month of the cleanup log

3. Extension, because the script is Mtime (last modified time) to delete, so can be used to delete expired backups, and so on, not subject to file name restrictions

Shell Tips

Find–type F–print will be based on the line or space to output the search file, under different sh have different responses, if not done with Xargs to delete the change operation, will have an impact

So you need to add –print0 use NULL as a boundary symbol to get married. Xargs–o to format input

Use Find to follow the minimum result set principle, find analytic form left to right, all make sure that your leftmost filter symbol is able to filter the maximum data

You can add to the crontab according to your needs.

Shell Script

Copy Code code as follows:

#!/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

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.