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中cookie知识点学习

什么是cookie cookie,即小饼干,是保存在用户代理端(浏览器是最常见的用户代理)的一些数据片段。浏览网页时,浏览器会将 当前页面有效的 cookie放在请求的头部发送到服务端。...

php 批量生成html,txt文件的实现代码

首先建立一个conn.php的文件用来链接数据库复制代码 代码如下:<?php    $link = mysql_connect("mysql_hos...

php中bind_param()函数用法分析

php中bind_param()函数用法分析

本文实例讲述了php中bind_param()函数用法。分享给大家供大家参考,具体如下: 从字面上不难理解,绑定的参数;下面我通过一个绑定参数的例子讲一下: for example: b...

php环境下利用session防止页面重复刷新的具体实现

b.php的代码 复制代码 代码如下: <?php //只能通过post方式访问 if ($_SERVER['REQUEST_METHOD'] == 'GET') {header(...

php smarty模版引擎中的缓存应用

1,Smarty缓存的配置: 复制代码 代码如下:$smarty->cache-dir="目录名"; //创建缓存目录名 $smarty->caching=true; //开...