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中利用str_pad函数生成数字递增形式的产品编号

解决办法:$str=”QB”.str_pad(($maxid[0]["max(id)"]+1),5,”0″,STR_PAD_LEFT ); 其中$maxid[0]["max(id)"]+...

PHP实现简单爬虫的方法

本文实例讲述了PHP实现简单爬虫的方法。分享给大家供大家参考。具体如下: <?php /** * 爬虫程序 -- 原型 * * 从给定的url获取html内容...

解析关于java,php以及html的所有文件编码与乱码的处理方法汇总

php文件中在乱码(如a.php文件在浏览器乱码):header("Content-Type:text/html;charset=utf-8")是设置网页的。mysql_query("s...

支持汉转拼和拼音分词的PHP中文工具类ChineseUtil

PHP 中文工具类,支持汉字转拼音、拼音分词、简繁互转。 PHP Chinese Tool class, support Chinese pinyin, pinyin participl...

apache+php完美解决301重定向的两种方法

幸好有301重定向能有效解决这样的问题。正如月光博客这篇文章中说的, 301重定向可促进搜索引擎优化效果 从搜索引擎优化角度出发,301重定向是网址重定向最为可行的一种办法。当网站的域名...