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中ltrim()函数的用法与实例讲解

PHP中ltrim()函数的用法与实例讲解

PHP ltrim() 函数 实例 移除字符串左侧的字符: <?php $str = "Hello World!"; echo $str . "<br>...

php中引用符号(&amp;)的使用详解

与C语言中的指针是有差别的.C语言中的指针里面存储的是变量的内容在内存中存放的地址变量的引用.PHP 的引用允许你用两个变量来指向同一个内容 复制代码 代码如下:$a="ABC"; $b...

用PHP读取flv文件的播放时间长度

复制代码 代码如下:<?php // +----------------------------------------------------------------------...

PHP通过iconv将字符串从GBK转换为UTF8字符集

1. iconv()介绍 iconv函数可以将一种已知的字符集文件转换成另一种已知的字符集文件。例如:从GB2312转换为UTF-8。 iconv函数在php5中内置,GB字符集默认打开...