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

相关文章

require(),include(),require_once()和include_once()的异同

require()和include()有许多相似之处,也有些不同。理解它们的不同点非常重要,否则很容易犯错误。  我把这两个语句放在一起介绍,读者可以比较学习。  1...

php 批量替换html标签的实例代码

1.把html元素全部去掉,或者保留某几个html标签复制代码 代码如下:<?php$text = '<p>Test paragraph.</p><!...

PHP+HTML+JavaScript+Css实现简单爬虫开发

PHP+HTML+JavaScript+Css实现简单爬虫开发

开发一个爬虫,首先你要知道你的这个爬虫是要用来做什么的。我是要用来去不同网站找特定关键字的文章,并获取它的链接,以便我快速阅读。 按照个人习惯,我首先要写一个界面,理清下思路。 &nbs...

php数组函数序列之array_unshift() 在数组开头插入一个或多个元素

array_unshift()定义和用法 array_unshift() 函数在数组开头插入一个或多个元素。 被加上的元素作为一个整体添加,这些元素在数组中的顺序和在参数中的顺序一样。...

PHP多进程简单实例小结

本文实例讲述了PHP多进程。分享给大家供大家参考,具体如下: PHP创建多进程需要使用到pcntl模块 在编译时加上--enable-pcntl打开进程控制支持,不是Unix类系统不支持...