当前位置: 移动技术网 > IT编程>开发语言>PHP > linux下删除7天前日志的代码(php+shell)

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

2019年04月24日  | 移动技术网IT编程  | 我要评论
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转换不熟悉

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网