PHP version:
Copy code The code is as follows:
/**
* Delete logs from 7 days ago
* @param $logPath
*/
function del7daysAgoLog($ logPath) {
if(empty($logPath))return;
$handle = opendir($logPath);
while(($file = readdir($handle)) !== false){
$pos = strpos($file, '.log');
if ($pos !== false && (strtotime("-1 week") > fileatime($logPath . $file))) {
unlink($logPath . $file);
}
}
}
shell version
Copy the code The code is as follows:
#!/bin/sh
function del7daysAgoLog (){
for file in $(ls $1)
do
if [ "${file##*.}" = "log" ]
then
ctime=$(stat $1/$file -c "%y")
ctimeU=$(date -d "$ctime" +%s)
now=$(date +%s)
SevenDaysAgo=$(($now - 36000 * $Days))
if [ $SevenDaysAgo -gt $ctimeU ]
then
$(rm $file)#Delete file here
fi
else
echo ""
fi
done
}
Days= 7
Path="/var/www/***/log"
del7daysAgoLog $Path $Days
The shell version is more troublesome. The key is that I am not familiar with linux conversion.
http://www.bkjia.com/PHPjc/322711.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322711.htmlTechArticlePHP version: Copy the code as follows: /*** Delete logs from 7 days ago * @param $logPath*/ function del7daysAgoLog($logPath) { if (empty($logPath))return; $handle = opendir($logPath);...