linux下删除7天前日志的代码(php+shell)

yipeiwu_com5年前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编程每天必学之表单验证

本文实例讲解了php表单验证的实现方法,分享给大家供大家参考,具体内容如下 1.PHP表单处理 welcome.html <html> <body> &l...

在PHP中输出JS语句以及乱码问题的解决方案

怎样在php中输出js语句? 示例 <?php $classState=""; if($state==0){ $classState="已下课"; } else{ $c...

配置php.ini实现PHP文件上传功能

昨天分享了在PHP网站开发中如何在php.ini中配置实现session功能的PHP教程,今天继续分享在利用PHP实现文件上传功能时几点关键php.ini的配置。   说到在php.in...

深入apache配置文件httpd.conf的部分参数说明

<Directory>...</Directory> -- 设定指定目录的访问权限<Files>...</Files> -- 设置应用于指...

详解在PHP的Yii框架中使用行为Behaviors的方法

一个绑定了行为的类,表现起来是这样的: // Step 1: 定义一个将绑定行为的类 class MyClass extends yii\base\Component { //...