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中return的用法实例分析

本文实例讲述了php中return的用法。分享给大家供大家参考。具体分析如下: 首先,它的意思就是返回;return()是语言结构而不是函数,仅在参数包含表达式时才需要用括号将其括起来。...

PHP音乐采集(部分代码)

<?PHP set_time_limit(0); mysql_connect("localhost","root",""); mysql_select_Db("music_36g"...

用php的ob_start来生成静态页面的方法分析

虽然方法很多,但使用起来简便容易的,我觉得还是先判断已经生成的首页文件的生成时间和现有时间之间的差值,如果满足某个值就开始生成,这种方法比较来得容易,不多说了,开始吧! 在开始之前还是提...

php中文字母数字验证码实现代码

英文同数字 <?php Header("Content-type:image/png"); //定义header,声明图片文件,最好是png,无版权之扰;  //生成新的...

php模拟登陆的实现方法分析

本文实例分析了php模拟登陆的实现方法。分享给大家供大家参考。具体分析如下: php模拟登陆的实现方法,这里分别列举两种方法实现模拟登陆人人网。具体实例代码如下: 1)使用snoopy模...