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+JS实现大规模数据提交的方法

本文实例讲述了PHP+JS实现大规模数据提交的方法。分享给大家供大家参考。具体实现方法如下: <?php session_start(); ?> <...

php preg_replace替换实例讲解

php preg_replace替换实例讲解

复制代码 代码如下:mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $l...

PHP防止图片盗用(盗链)的方法小结

本文实例总结了PHP防止图片盗用(盗链)的方法。分享给大家供大家参考,具体如下: 图片防盗链有什么用? 防止其它网站盗用你的图片,浪费你宝贵的流量。本文章向大家介绍php防止图片...

启用Csrf后POST数据时出现的400错误

最近一直出现这样的错误,一直在查找原因,偶然看到一篇解决的文章,分享给大家看看。 第一种解决办法是关闭Csrf public function init(){ $this->...