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转换不熟悉

相关文章

phpstudy默认不支持64位php的解决方法

备忘一下: windows上用phpstudy比较简便,但是其默认的php所有版本都是32位的,有坑,比如int最大值。 所以从php官网 点击打开链接http://windows.ph...

理解php依赖注入和控制反转

要想理解php依赖注入和控制反转两个概念,就必须搞清楚如下的问题:  DI——Dependency Injection   依赖注入 IoC——Invers...

php下获取http状态的实现代码

逐风整理了两种方式,大家可以自行参考/使用: 复制代码 代码如下:#方式一$ch = curl_init('//www.jb51.net');curl_setopt($ch, CURL...

PHP常用技巧总结(附函数代码)

PHP文件读取函式 复制代码 代码如下: //文件读取函式 function PHP_Read($file_name) { $fd=fopen($file_name,r); while(...

php判断两个日期之间相差多少个月份的方法

本文实例讲述了php判断两个日期之间相差多少个月份的方法。分享给大家供大家参考。具体实现方法如下: /** * @author injection(injection.mail@g...