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操作xml入门之xml标签的属性分析

php操作xml入门之xml标签的属性分析

本文实例讲述了php操作xml入门之xml标签的属性。分享给大家供大家参考。具体分析如下: 复制代码 代码如下:<?xml version="1.0" encoding="...

php中sprintf与printf函数用法区别解析

下面是一个示例:四舍五入保留小数点后两位 复制代码 代码如下:<?php$num1 = 21;echo sprintf("%0.2f",$num1)."<br />";...

Yii2框架整合Xunsearch搜索引擎的方法

本文实例讲述了Yii2框架整合Xunsearch搜索引擎的方法。分享给大家供大家参考,具体如下: 公司一直用的YII2框架,然后要做一个中文搜索引擎,所有想的Xunsearch这个项目,...

php基于jquery的ajax技术传递json数据简单实例

本文实例讲述了php基于jquery的ajax技术传递json数据简单实现方法。分享给大家供大家参考,具体如下: html页面: <html> <head>...

总结的一些PHP开发中的tips(必看篇)

一、开发习惯和php代码 1、准确的理解各种概念。现在的新东西层出不穷,望文生义和一知半解对开发工作有害无益;//比如我就碰到有人理解松散耦合(这个东西不新)的概念居然是要求代码不要有空...