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实现的oracle分页函数实例

本文实例讲述了PHP实现的oracle分页函数。分享给大家供大家参考,具体如下: mysql有limit,分页的时候,我们可以用limit 30,40,而oracle没有limit,用其...

PHP生成可点击刷新的验证码简单示例

本文实例讲述了PHP生成可点击刷新的验证码。分享给大家供大家参考,具体如下: html文件: <html> <head> <title>验...

用PHP实现弹出消息提示框的两种方法

方法一:复制代码 代码如下:echo "<script>alert('提示内容')</script>";方法二:复制代码 代码如下:echo '<scrip...

php截取字符串并保留完整xml标签的函数代码

复制代码 代码如下:<?php      /**      * author: goosman &n...

php版微信自定义回复功能示例

php版微信自定义回复功能示例

本文实例讲述了php版微信自定义回复功能。分享给大家供大家参考,具体如下: 配置好服务器之后,就可以用php实现自动回复了。 index.php中的代码 <?php...