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 /** * * @author Administrator * */ cl...

php 来访国内外IP判断代码并实现页面跳转

我大概构思了一下,有两个方案: 1. Javascript判断来访者的浏览器语言,如果是中文系统,自然使用者都是中国人,跳中文网站; 如果是非中文系统,默认使用者非中国人,跳英文网站。...

解决安装WampServer时提示缺少msvcr110.dll文件的问题

解决安装WampServer时提示缺少msvcr110.dll文件的问题

今天开始学习PHP,对于初学者来说,我们一定希望从简单的开始,所以,从集成环境非常好的WampServer的安装开始. 1、下载WampServer安装程序,安装完毕后会出现一个错误.如...

php写入文件不覆盖的实例讲解

php写入文件不覆盖的实例讲解

file_put_contents():向文件中写入内容并且不覆盖之前的内容。 步骤: 1、新建文件 2、声明要写入内容的文件 3、这个文件的内容如图 4、file_get_con...

pdo中使用参数化查询sql

方法 bindParam() 和 bindValue() 非常相似。 唯一的区别就是前者使用一个PHP变量绑定参数,而后者使用一个值。 所以使用bindParam是第二个参数只能用变量名...