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

相关文章

PHPwind整合最土系统用户同步登录实现方法

上次成功升级了最土商业版,接下来就是整合公司的社区网站,先说明一下我现在工作的地方是个地方社区网站,用的基础程序是PHPWind,我的任务就是让PHPWind和最土登录同步,领导也知道我...

php中preg_replace_callback函数简单用法示例

本文实例讲述了php中preg_replace_callback函数用法。分享给大家供大家参考,具体如下: mixed preg_replace_callback ( mixed pat...

解析thinkphp基本配置 convention.php

复制代码 代码如下:return  array(     /* 项目设定 */    'APP_DEBUG'&nbs...

php使用function_exists判断函数可用的方法

本文实例讲述了php使用function_exists判断函数可用的方法。分享给大家供大家参考。具体如下: 本文所述的函数用来建立一张 gif 格式图形,参数 im 为使用 imagec...

PHP设计模式之装饰者模式

PHP设计模式之装饰者模式

介绍 装饰者模式动态地将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。 思维导图   有这样一个项目,做一个餐厅订餐系统。起初的代码结构是这样的。前...