Shell script for regular cleanup of log files by "Shell combat"

Source: Internet
Author: User
Tags disk usage

Function Description: Cleans the Messages history log file (Messages-date) under the/var/log/path, but does not clean the messages file itself

Dependency requirements: The BC module is installed on the server

1 #clean_logs.sh2 #!/bin/bash3 4 # ==========================================5 #function: Cleans the Messages history log file (Messages-date) under the/var/log/path, but does not clean the messages file itself6 #method: Perform daily with crontab as a scheduled task7 #return: None8 #Warning: Prohibit direct deletion of/var/log/messages files9 #Date: 2018-04-23Ten #By:hackhan One #others: None A # ========================================== -  - #create a file to save history log purge records/opt/cleanlogs/clean_log.log the Createlogfile () - { -Targetdir=$1 -Targetfile=$2 +  -         if[ ! -d $targetDir];then +mkdir-P $targetDir A fi at  -         if[ ! -F $targetFile];then - Touch $targetFile - fi - } -  in #writing information to the log file - Logtofile () to { +Msg=$1 -Targetfile=$2 theEcho $msg >>$targetFile * } $ Panax Notoginseng #gets the root disk usage and remaining free space size, returned as an array - getrootdirusagerate () the { +Usagepcent= ' DF/h | Awk'$6== "/" {sub ("%", "" ", $ $);p Rint}'` Aavaildisk= ' DF/h | Awk'$6== "/" {print $4}'` thearray=($usagepcent $availDisk) +Echo ${array[*]} - } $  $ #determine compliance According to the actual disk usage, actual available size, usage threshold, expected available disk space threshold, as entered - #returns 1 for compliance, and 0 for non-conforming requirements - Isdiskusageok () the { -         #actual utilization, minus the number after%WuyiUsagepcent=$1 the         #actual available disk space, unit may be k/m/g -Availdisk=$2 Wu         #usage threshold, minus the number after% -Threshholdpcent=$3 About         #Available disk space thresholds, no units (for G) $Threshholdavaildisk=$4 -  -         if[${availdisk:0-1} ="g"-O ${availdisk:0-1} ="G" ] -  Then AAvaildisknounit= ' echo $availDisk |awk'{sub (/.$/, "")}1'` +  the                 if[' Echo"$usagepcent > $threshHoldPcent"| BC '-EQ 1] | | [' Echo"$availDiskNoUnit < $threshHoldAvailDisk"| BC '-EQ 1 ] -  Then $Echo"0" the                 Else theEcho"1" the fi the  -         Else inEcho"0" the fi the  About } the  the #the generated log directory theTargetdir="/opt/cleanlogs/" + #the generated log file -targetfile="${targetdir}clean_log.log" the #The path where the file is to be deletedBayiTargetlogdir="/opt/shell/logs/" the # targetlogdir= "/var/log/" the  - #Available disk space thresholds, units default to G -availdiskthresh=10 the #disk usage, unit default is% theDiskusagepcent=90 the  the  - createlogfile $targetDir $targetFile the  theresult=(' getrootdirusagerate ') the 94Begincleantime= ' Date'+%y-%m-%d%h:%m:%s'` the  theLogtofile"==========================================="$targetFile theLogtofile"[* * *] scanning time: ${begincleantime}"$targetFile98Logtofile"/Disk info:avail:${result[1]} use%:${result[0]}%"$targetFile AboutLogtofile"-------------------------------------------"$targetFile - 101 102 103Isok= ' Isdiskusageok ${result[0]} ${result[1]} $diskUsagePcent $availDiskThresh '104  the if[$isOK ="0" ]106  Then107 108Logtofile"[+] starting to clean logs ..."$targetFile109  the          forIteminch' LS ${targetlogdir}messages-*';d o111                 #rm-f $item the Echo $item113 logtofile $item $targetFile the  Done the  theLogtofile"[-] Ended cleaning logs!"$targetFile117 fi118 119Endcleantime= ' Date'+%y-%m-%d%h:%m:%s'` - 121Logtofile"[* * *] Ended time: ${endcleantime}"$targetFile122 123Logtofile"==========================================="$targetFile124Logtofile""$targetFile

To implement timed execution, you need to create a crontab plan. For example, we want to execute the log cleaner every 1 hours, assuming that the executable file has a path of/opt/shellutils/clean_logs.sh, then create Crontab as follows:

# CRONTAB-E
* */1 * * */opt/shellutils/clean_logs.sh >>/opt/cleanlogs/clean_log.log 2>&1

Shell script for regular cleanup of log files by "Shell combat"

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.