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中的几个时间处理类:DateTime,DateTimeZone,DateInterval,DatePeriod...

PHP 彩色文字实现代码

最近流行彩字,下面是简单的实现方法: 一.彩字的简单实现 复制代码 代码如下:header("content-type: image/png"); $text = $_get['t'];...

PHPExcel导出2003和2007的excel文档功能示例

本文实例讲述了PHPExcel导出2003和2007的excel文档功能。分享给大家供大家参考,具体如下: require_once 'common/excel/PHPExcel.p...

php恢复数组的key为数字序列的方法

本文实例讲述了php恢复数组的key为数字序列的方法。分享给大家供大家参考。具体分析如下: 这里实现php把数组的key值恢复成类似于0,1,2,3,4,5...这样的数字序列 fu...

PHP获取中英混合字符串长度的方法

今晚在写框架的表单验证类时,需要判断某个字符串长度是否在指定区间内,很自然地,想到了PHP中的strlen函数。复制代码 代码如下:$str = 'Hello world!';echo...