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

相关文章

MAC下通过改apache配置文件切换php多版本的方法

前言 前段时间,在自己的电脑上升级了php,php7.0虽然有部分更新,速度也提升了不少,但最近在做微信开发时,发现很多引擎不支持php7,于是想能不能安装两个版本进行切换,百度了很多方...

PHP base64+gzinflate压缩编码和解码代码

base64+gzinflate压缩编码(加密)过的文件通常是以 <? eval(gzinflate(base64_decode( 为头的一个php文件。以下我们给出了相关的编码解...

ThinkPHP 模板substr的截取字符串函数详解

ThinkPHP 模板substr的截取字符串函数详解

ThinkPHP 模板substr的截取字符串函数 在Common/function.php加上以下代码 /** ** 截取中文字符串 **/ function msubstr(...

php中计算中文字符串长度、截取中文字符串的函数代码

在PHP中,我们都知道有专门的mb_substr和mb_strlen函数,可以对中文进行截取和计算长度,但是,由于这些函数并非PHP的核心函数,所以,它们常常有可能没有开启。当然,如果是...

培养自己的php编码规范

培养自己的php编码规范

为什么我们要培养自己的编码规范? 我们写代码的时候,一个好的编码规范,对我们来说能够起到很多意向不到的效果。至少会有一下的好处: 1、提高我们的编码效率。整齐划一的代码方便我们进行复制...