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简单开启curl的方法(测试可行) 原创

PHP简单开启curl的方法(测试可行) 原创

本文讲述了PHP简单开启curl的方法。分享给大家供大家参考,具体如下: 一、问题: windows主机出现“Call to undefined function curl_init”错...

基于php中echo用逗号和用点号的区别详解

基于php中echo用逗号和用点号的区别详解

实例如下: <?php //点和逗号的测试,涉及到字符串的强制转换 echo 1+5; echo "<br /><br />"; echo '...

按上下级层次关系输出内容的PHP代码

复制代码 代码如下:function getSubComments($parent = 0, $level = 0) { $db = &JFactory::getDBO(); $sql...

php在字符串中查找另一个字符串

<a href="./">返回列表</a><br> <form action="<?echo $PHP_SELF;?>" metho...

浅谈PHP无限极分类原理

浅谈PHP无限极分类原理

1.递归:程序调用自身的编程技巧称为递归 2.案例: /** * @param 递归 $[name] */ function deeploop(&$i=1){ echo...