清理服務器網站日志


1.清空nginx站點日志的內容(如果刪除日志文件,只有重啟服務器才能重新生成日志文件進行記錄)

編輯腳本

 

 

 

添加定時任務,每月的14號和28號的12點0分執行清理腳本

 

 

2.刪除iis站點過期的日志(我的日志是天計划,另外一點需要注意的是不能刪除當天的日志,因為會有警告說已經在系統打開)

<?php
/* 
    清理IIS網站過期日志,釋放C盤空間 
*/

$logs_path = "C:\inetpub\logs\LogFiles";    //日志所在路徑
$folder_head = "W3SVC";    //日志文件夾名的前綴
$file_head = "u_ex";    //日志文件名的前綴
$file_foot = ".log";    //日志文件名的后綴

$logs_path = str_replace("\\", "/", $logs_path);
if (! file_exists($logs_path)) die('日志目錄不存在');
$scan = scandir($logs_path);
$folders = array();    //定義變量存在日志文件夾名

for ($i=0; $i < count($scan); $i++) { 
    if((substr($scan[$i], 0, strlen($folder_head)) == $folder_head) && file_exists($logs_path.'/'.$scan[$i]))
        array_push($folders, $scan[$i]);
}

$today = date('Ymd');
$today = substr($today, 2);
$todayLog = $file_head.$today.$file_foot;


for ($i=0; $i < count($folders); $i++) { 

    $filesInFolder = scandir($logs_path.'/'.$folders[$i]);

    for ($j=0; $j < count($filesInFolder); $j++) { 
        if(($filesInFolder[$j] != $todayLog) && 
            (substr($filesInFolder[$j], 0, strlen($file_head)) == $file_head) &&
            (strrev(substr(strrev($filesInFolder[$j]), 0,strlen($file_foot))) == $file_foot))
            unlink($logs_path.'/'.$folders[$i].'/'.$filesInFolder[$j]);
    }
}

echo '過期日志清理完畢';

?>

編輯刪除腳本 clear.php

 

編輯bat批處理文件,存放位置任意,雙擊該文件即可完成刪除,此處不做定時任務,手動刪除

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM