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计算两个坐标(经度,纬度)之间距离的方法

本文实例讲述了php计算两个坐标(经度,纬度)之间距离的方法。分享给大家供大家参考。具体如下: 这里使用php计算两个坐标(经度,纬度)之间的距离,返回结果为米或者千米 functi...

php基于自定义函数记录log日志方法

本文实例讲述了php基于自定义函数记录log日志方法。分享给大家供大家参考,具体如下: /** * 记录错误日志 * @param 日志内容 $res */ function...

php生成curl命令行的方法

本文实例讲述了php生成curl命令行的方法。分享给大家供大家参考,具体如下: 示例: curl "http://localhost/other/serverInfo.php?d...

如何批量替换相对地址为绝对地址(利用bat批处理实现)

如果你的url链接是相对路径“static/mapi.css”,你想把他批量替换成绝对路径“http://dev.baidu.com/wiki/static/map/cloud/stat...

php Xdebug 调试扩展的安装与使用.

Xdebug安装 下载xdebug扩展的时候注意xdebug的版本与php版本相对应,不然出现不必要的麻烦! 我自己使用的是PHP5.2.5,下载的xdebug文件名为:php_xdeb...