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使用gettimeofday函数返回当前时间并存放在关联数组里

本文实例讲述了php使用gettimeofday函数返回当前时间并存放在关联数组里的方法。分享给大家供大家参考。具体分析如下: 英文官方描述如下: Key Descriptio...

深入讲解PHP Session及如何保持其不过期的方法

SESSION的实现中采用COOKIE技术,SESSION会在客户端保存一个包含session_id(SESSION编号)的COOKIE;在服务器端保存其他session变量,比如ses...

PHP 7的一些引人注目的新特性简单介绍

1. ?? 运算符(NULL 合并运算符) 把这个放在第一个说是因为我觉得它很有用。用法: $a = $_GET['a'] ?? 1; 它相当于:...

PHP常用数组函数介绍

在编程中查手册是少不了的,所以要会学着使用已有的东西,就如PHP中的数组处理函数已经有排序函数了,为什么还要在写东西是费着劲去写冒泡或者堆排或者快排呢。   编程是间接的过程,也是重用的...

PHP调用存储过程返回值不一致问题的解决方法分析

本文实例讲述了PHP调用存储过程返回值不一致问题的解决方法。分享给大家供大家参考,具体如下: 今天遇一个同学聊存储过程返回值经常得到意外的值为null, 因为白天有事,晚上给做一个实验放...