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 $_SERVER["REQUEST_URI"]获取值的通用解决方法

复制代码 代码如下: <?php // 说明:获取 _SERVER['REQUEST_URI'] 值的通用解决方案 function request_uri() { if (iss...

PHP7.1新功能之Nullable Type用法分析

本文实例分析了PHP7.1新功能之Nullable Type用法。分享给大家供大家参考,具体如下: 在 PHP5 时代,PHP 的参数已经支持 type hint(除了基本类型),想必大...

解析php5配置使用pdo

1. 检查php扩展库中是否存在php_pdo.dll(当调用MsSQL同时还需要php_pdo_mssql.dll;当调用MySQL同时还需要php_pdo_mysql.dll). 2...

兼容PHP5的PHP目录管理函数库

主要能兼容: PHP 5 一、chdir -- 改变目录 语法:bool chdir ( string d...

php判断正常访问和外部访问的示例

php判断正常访问和外部访问 复制代码 代码如下: <?php session_start(); if(isset($_POST['check'])&&!empty($_POST[...