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使用cookie保存用户登录的用户名实例

本文实例讲述了php使用cookie保存用户登录的用户名的方法。分享给大家供大家参考。具体实现方法如下: 用户登录文件:login.php 复制代码 代码如下:<html>...

php addslashes及其他清除空格的方法是不安全的

清除空格的方法是不安全的,部分原因是因为字符中的空格非常多,例如 "addslashes的问题在 于黑客 可以用0xbf27来代替单引号,而addslashes只是将0xbf27修改为0...

探讨GDFONTPATH能否被winxp下的php支持

php学习中遇一问题,使用GD库绘图,设置字体路径变量:putenv('GDFONTPATH=c:\windows\Fonts');$fontname='arial';$bbox=ima...

php中神奇的fastcgi_finish_request

听起来可能有些茫然,我们通过几个例子来说明一下: 复制代码 代码如下: <?php echo '例子:'; fastcgi_finish_request(); echo 'To b...

php中session_unset与session_destroy的区别分析

session_unset() 释放当前在内存中已经创建的所有$_SESSION变量,但不删除session文件以及不释放对应的session id session_destroy()...