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实现找出链表中环的入口节点。分享给大家供大家参考,具体如下: 问题 一个链表中包含环,请找出该链表的环的入口结点。 解决思路 第一步,找环中相汇点。分别用p1,p2指...

探讨多键值cookie(php中cookie存取数组)的详解

cookie默认不能存数组,所以下面的写法是错误的。报错如下:Warning: setcookie() expects parameter 2 to be string, array g...

CodeIgniter图像处理类的深入解析

image.php 复制代码 代码如下:<?phpclass Image extends Controller {    function Image...

php下过滤html代码的函数 提高程序安全性

以下为过滤HTML代码的函数: 复制代码 代码如下: function ihtmlspecialchars($string) { if(is_array($string)) { fore...

PHP实现字符串的全排列详解

PHP实现字符串的全排列详解

输入一个字符串,按字典序打印出该字符串中字符的所有排列。 例如,输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 思路...