#!/alidata/server/php/bin/php false, 'timeOut' => 7200 )); function echoInfo($content) { $content = date('Y-m-d H:i:s') . " $content\r\n"; echo $content; } function qExec($cmd) { $outputArray = array(); $returnVar = ''; exec($cmd, $outputArray, $returnVar); $output = empty($outputArray) ? '' : implode(', ', $outputArray); return [$returnVar, $output]; } $saveLogDays = 5; $diskUsageRateCmd = "df -h | grep '/dev/vda' | awk '{print $5}' | awk -F '%' '{print $1}'"; if (gethostname() == 'honor-s1') { $diskUsageRateCmd = "df -h | grep '/dev/xvda' | awk '{print $5}' | awk -F '%' '{print $1}'"; } $diskUsageRate = shell_exec($diskUsageRateCmd); $diskUsageRate = (int)$diskUsageRate; if ($diskUsageRate < 85) { echoInfo("diskUsageRate: {$diskUsageRate}%, less then 85%, end."); return ; } if ($diskUsageRate > 90) { $saveLogDays = 1; } echoInfo("start clear log files, diskUsageRate: $diskUsageRate."); // 清理saveLogDays天前的日志 if ($diskUsageRate > 98) { $cmd = 'find /alidata/log/httpd /alidata/log/*biz -type f -exec rm -rf {} \;'; } else { $cmd = 'find /alidata/log/httpd /alidata/log/*biz -type f -mtime +' . $saveLogDays . ' -exec rm -rf {} \;'; } list($returnVar, $output) = qExec($cmd); echoInfo("clear log [$cmd], returnVar [$returnVar], output [$output]"); // 清理httpd的error_log $cmd = 'echo "" > /alidata/server/httpd/logs/error_log'; list($returnVar, $output) = qExec($cmd); echoInfo("clear httpd error_log [$cmd], returnVar [$returnVar], output [$output]"); // 清空所有的crontab文件 $files = glob('/alidata/log/crontab/*.log', GLOB_BRACE); foreach($files as $file) { if (stripos($file, 'clear_log') !== false) { continue; } $cmd = "echo '' > $file"; list($returnVar, $output) = qExec($cmd); echoInfo("clear log [$cmd], returnVar [$returnVar], output [$output]"); } $cmd = '/usr/sbin/tmpreaper 2h /tmp'; list($returnVar, $output) = qExec($cmd); echoInfo("clear log [$cmd], returnVar [$returnVar], output [$output]"); $diskUsageRate = shell_exec($diskUsageRateCmd); $diskUsageRate = (int)$diskUsageRate; echoInfo("end clear log files, diskUsageRate: $diskUsageRate.");