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中DateTime的常用方法

前言 实例化对象前面加\表示的是,在命名空间中使用原生的类,如果没有使用命名空间的话,可以把前面的\给删除掉 1. 输出当前时间 $datetime = new \DateTime;...

php获取当前月与上个月月初及月末时间戳的方法

本文实例讲述了php获取当前月与上个月月初及月末时间戳的方法。分享给大家供大家参考,具体如下: 当前月 <?php $thismonth = date('m'); $t...

PHP中判断文件存在使用is_file还是file_exists?

判断文件存在用is_file还是file_exists? 在写程序时发现在判断文件是否存在时,有两种写法,有的人用了is_file,有的人用了file_exists,用哪个更好或者说更合...

解决ThinkPHP下使用上传插件Uploadify浏览器firefox报302错误的方法

最近用ThinkPHP开发一个项目,集成了批量上传文件插件Uploadify,在谷歌Chrome和IE下都能正常上传,只有火狐下提示这个错误,网上找了很多解决办法,基本都说flash在f...