[Shell combat] Shell script to regularly clean up log files

Function description: Clean the message history log file (messages-date) under the /var/log/ path, but not the message file itself

Dependency requirements: The bc module is installed on the server

  1  # clean_logs.sh 
  2  # !/bin/bash 
  3  
  4  # ===================================== ======= 
  5  #Function : Clean up the message history log file (messages-date) under the /var/log/ path, but do not clean up the messages file itself 
  6  #Method : Work with crontab as a scheduled task to execute every 
day   7  #Return :None 
  8  #Warning : Direct deletion of /var/log/messages file is prohibited 
  9  #Date :2018-04-23 
10    #By:HackHan 11 #Other : None 12 # ============= ============================== 13 14 #Create a file /opt/cleanlogs/clean_log.log to save the historical log cleanup records
 
 
 
 
15  createLogFile()
 16  {
 17          targetDir=$1
 18          targetFile=$2
 19  
20          if [ ! - d $targetDir ];then
 21                  mkdir - p $targetDir
 22          fi
 23  
24          if [ ! - f $targetFile ];then
 25                  touch $targetFile
 26          fi
 27  }
 28  
29  #Write information to the log file 
30  logToFile()
 31  {
 32          msg=$1
 33         targetFile=$2
 34          echo $msg >> $targetFile
 35  }
 36  
37  #Get root directory disk usage and remaining free space size, return 38 in array form 
getRootDirUsageRate()
 39 {
 40          usagepcent=`df / -h | awk ' $6 =="/"{sub("%","",$5);print $5} ' `
 41          availDisk=`df / -h | awk ' $6=="/"{print $4} ' `
 42          array= ( $usagepcent $availDisk)
 43          echo ${array[* ]}
 44 }
 45 46 #    
 
According to the input actual disk usage
 ,
 actual available size, usage threshold, and expected available disk space threshold to determine whether the requirements are met  , the number after % is removed 51          usagepcent=$1
 52 #Actual available disk space, the unit may be K/M/G 53          availDisk=$2
 54 #Utilization threshold, the number after % is removed 55          threshHoldPcent=$3
 56 #The threshold of available disk space , unitless (for G) 57          threshHoldAvailDisk=$4
 58 59 if [ ${availDisk:0-1} = " g " -o ${availDisk:0-1} = " G "
           
         
         
         
 
          ]
 60         then
 61                 availDiskNoUnit=`echo $availDisk|awk '{sub(/.$/,"")}1'`
 62 
 63                 if [ `echo "$usagepcent > $threshHoldPcent" | bc` -eq 1 ] || [ `echo "$availDiskNoUnit < $threshHoldAvailDisk" | bc` -eq 1 ]
 64                 then
 65                         echo "0"
 66                 else
 67                         echo "1"
                fi
68 69  
70          else 
71                  echo " 0 " 
72          fi
 73  
74  }
 75  
76  #Generated log directory 
77 targetDir= " /opt/cleanlogs/ " 
78  #Generated log file 
79 targetFile= " ${targetDir}clean_log.log " 
80  # The path where the file to be deleted is located 
81 targetLogDir= " /opt/shell/logs/ " 
82  # targetLogDir="/var/log/" 
83  
84  #The threshold of available disk space, the default unit is G
 85 availDiskThresh=10
 86 # 磁盘使用率,单位默认为%
 87 diskUsagePcent=90
 88 
 89 
 90 createLogFile $targetDir $targetFile
 91 
 92 result=(`getRootDirUsageRate`)
 93 
 94 beginCleanTime=`date '+%Y-%m-%d %H:%M:%S'`
 95 
 96 logToFile "===========================================" $targetFile
 97 logToFile "[***] Scanning time : ${beginCleanTime}" $targetFile
 98 logToFile "/ Disk info: Avail:${result[1]}      Use%:${result[0]}%" $targetFile
 99 logToFile "-------------------------------------------" $targetFile
100 
101 
102 
103 isOK=`isDiskUsageOK ${result[0]} ${result[1]} $diskUsagePcent $availDiskThresh`
104 
105 if [ $isOK = "0" ]
106 then
107 
108         logToFile "[+] Starting to clean logs ..." $targetFile
109 
110         for item in `ls ${targetLogDir}messages-*`;do
111                 #rm -f $item
112                 echo $item
113                 logToFile $item $targetFile
114         done
115 
116         logToFile "[-] Ended cleaning logs !" $targetFile
117 fi
118 
119 endCleanTime=`date '+%Y-%m-%d %H:%M:%S'`
120 
121 logToFile "[***] Ended time: ${endCleanTime}" $targetFile
122 
123 logToFile "===========================================" $targetFile
124 logToFile "" $targetFile

To achieve scheduled execution, you need to create a crontab plan. For example, we want to execute the log cleaning program every hour. Assuming that the executable file is stored in the path /opt/shellutils/clean_logs.sh, then create a crontab as follows:

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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325172059&siteId=291194637