linux下删除7天前日志的代码(php+shell)

yipeiwu_com6年前PHP代码库
PHP版本:
复制代码 代码如下:

/**
* 删除7天前的日志
* @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 版本
复制代码 代码如下:

#!/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)#此处删除文件
fi
else
echo ""
fi
done
}
Days=7
Path="/var/www/***/log"
del7daysAgoLog $Path $Days


shell 版本比较麻烦 关键我linux转换不熟悉

相关文章

PHP面向对象之旅:深入理解static变量与方法

static关键字声明一个属性或方法是和类相关的,而不是和类的某个特定的实例相关,因此,这类属性或方法也称为“类属性”或“类方法”。 如果访问控制权限允许,可不必创建该类对象而直接使用类...

php curl选项列表(超详细)

第一类:对于下面的这些option的可选参数,value应该被设置一个bool类型的值:选项可选value值备注CURLOPT_AUTOREFERER当根据Location:重定向时,自...

解析php取整的几种方式

floor 舍去法取整 语法格式:float floor ( float value )返回不大于value 的下一个整数,将value 的小数部分舍去取整。floor() 返回的类型仍...

php单例模式实现方法分析

本文实例讲述了php单例模式实现方法。分享给大家供大家参考。具体如下: <?php /** * @copyright 2013 maguowei.com * @au...

PHP实现图片自动清理的方法

本文实例讲述了PHP实现图片自动清理的方法。分享给大家供大家参考。具体实现方法如下: <?php /** * 图片清理计划程序,删除文件下两周没有访问的文件 */...