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 Try-catch 语句使用技巧

PHP Try-catch 语句 为了进一步处理异常,我们需要使用try-catch语句----包括Try语句和至少一个的catch语句。任何调用 可能抛出异常的方法的代码都应该使用tr...

PHP-redis中文文档介绍

phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://...

php获取用户浏览器版本的方法

本文实例讲述了php获取用户浏览器版本的方法。分享给大家供大家参考。具体分析如下: 在php中我们有个全局变量$_SERVER['HTTP_USER_AGENT'];可以获取用户所有信息...

PHP pear安装配置教程

什么是PEAR? PEAR是PHP扩展与应用库(the PHP Extension and Application Repository)的缩写。它是一个PHP扩展及应用的一个代码仓库,...

php查询操作实现投票功能

php查询操作实现投票功能

本文实例为大家分享了php查询操作实现投票功能的代码,供大家参考,具体内容如下 题目: 解题方法汇总: 方法一: 1. 投票主页面: <!DOCTYPE html PU...