手机
当前位置:查字典教程网 >编程开发 >php教程 >linux下删除7天前日志的代码(php+shell)
linux下删除7天前日志的代码(php+shell)
摘要:PHP版本:复制代码代码如下:/***删除7天前的日志*@param$logPath*/functiondel7daysAgoLog($lo...

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转换不熟悉

【linux下删除7天前日志的代码(php+shell)】相关文章:

域名查询代码公布

php将字符串随机分割成不同长度数组的方法

PHP邮件专题

十天学会php(3)

删除无限级目录与文件代码共享

我的论坛源代码(四)

我的论坛源代码(十)

我的论坛源代码(九)

我的论坛源代码(一)

如何限制访问者的ip(PHPBB的代码)

精品推荐
分类导航