加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 05:49:15 所属栏目:PHP教程 来源:网络整理
导读:PHP版本: div class="codetitle" a style="CURSOR: pointer" data="84006" class="copybut" id="copybut84006" onclick="doCopy('code84006')" 代码如下: div class="codebody" id="code84006" / 删除7天前的日志 @param $logPath / function del7daysAgoLo

PHP版本:
<div class="codetitle"><a style="CURSOR: pointer" data="84006" class="copybut" id="copybut84006" onclick="doCopy('code84006')"> 代码如下:<div class="codebody" id="code84006">
/
删除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 版本
<div class="codetitle"><a style="CURSOR: pointer" data="75720" class="copybut" id="copybut75720" onclick="doCopy('code75720')"> 代码如下:<div class="codebody" id="code75720">
#!/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转换不熟悉

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读