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正则过滤html标签、空格、换行符的代码(附说明)

复制代码 代码如下: $str=preg_replace("/\s+/", " ", $str); //过滤多余回车 $str=preg_replace("/<[ ]+/si","...

ThinkPHP删除栏目(实现批量删除栏目)

ThinkPHP删除栏目(实现批量删除栏目)

前段时间发表了一个删除栏目的随笔,当时实现的功能是删除一条信息,这次来实现一下批量删除栏目。 我们需要达到的是这样一个效果: 选中批量删除按钮后可以选中所有该页面的栏目,这个是前端页面...

rrmdir php中递归删除目录及目录下的文件

复制代码 代码如下: function rrmdir($dir) { if (is_dir($dir)) { $objects = scandir($dir); foreach ($ob...

PHP.ini安全配置检测工具pcc简单介绍

PHP.ini安全配置检测工具pcc简单介绍

概述 前一段时间,在工作中遇到了一个开源程序,该程序主要用来检测PHP配置文件中得配置项是否存在安全隐患,并提出相应的配置建议,使PHP程序更加安全。 使用 这个程序使用起来非常简单,大...

php对关联数组循环遍历的实现方法

本文实例讲述了php对关联数组循环遍历的实现方法。分享给大家供大家参考。具体分析如下: php对于类似 $age = array("zhangshan"=>14,"lisi"...