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中将一个字符串部分字符用星号*替代隐藏的实现代码

有时候我们在开发中会遇到这样一种情况,例如:显示手机号我们需要将中间4位遮挡掉,一般使用“*”号代替,或是显示身份证号码是为了保护个人信息也同样需要遮挡掉4位,故可用到下列方式、代码进行...

PHP/HTML混写的四种方式总结

PHP/HTML混写的四种方式总结

PHP作为一款后端语言,为了输出给浏览器让浏览器呈现出来,无可避免的要输出HTML代码,下文介绍下我用过的三种PHP/HTML混编方法 1、单/双引号包围法 这是最初级的方法了,用法就像...

PHP5.5在windows安装使用memcached服务端的方法

PHP5.5在windows安装使用memcached服务端的方法

  PHP5.5 在windows下安装 memcached 的方法   下载服务端资源   http://download.csdn.net/detail/zsjangel/71047...

PHP严重致命错误处理:php Fatal error: Cannot redeclare class or function

1、错误类型:PHP致命错误 Error type: PHP Fatal error Fatal error: Cannot redeclare (a) (previously decl...

php文件压缩之PHPZip类用法实例

本文实例讲述了php文件压缩之PHPZip类用法。分享给大家供大家参考。具体如下: <?php // // PHPZip v1.2 by Sext (sext@neud...