源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-03-31 09:55 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:linux下删除7天前日志的代码(php+shell)
PHP版本:
[u]复制代码[/u] 代码如下:
/** * 删除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 版本
[u]复制代码[/u] 代码如下:
#!/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转换不熟悉
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部