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 array_multisort() 函数的深入解析

一、先看最简单的情况。有两个数组:$arr1 = array(1,9,5);$arr2 = array(6,2,4);array_multisort($arr1,$arr2);print...

ini_set的用法介绍

PHP ini_set用来设置php.ini的值,在函数执行的时候生效,脚本结束后,设置失效。无需打开php.ini文件,就能修改配置,对于虚拟空间来说,很方便。 函数格式:string...

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

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

更改localhost为其他名字的方法

ctrl + r => 输入drivers回车 => etc/hosts , 用记事本打开它,在 127.0.0.1 localhost 下面增加一行, 127.0.0.1...