删除7天前,日志
时间:2010-12-30 来源:Jason-zy
1 /**
2 * 删除7天前的日志
3 * @param $logPath
4 */
5 function del7daysAgoLog($logPath) {
6 if(empty($logPath))return;
7 $handle = opendir($logPath);
8 while(($file = readdir($handle)) !== false){
9 $pos = strpos($file, '.log');
10 if ($pos !== false && (strtotime("-1 week") > fileatime($logPath . $file))) {
11 unlink($logPath . $file);
12 }
13 }
14 }
2 function del7daysAgoLog (){
3 for file in $(ls $1)
4 do
5 if [ "${file##*.}" = "log" ]
6 then
7 ctime=$(stat $1/$file -c "%y")
8 ctimeU=$(date -d "$ctime" +%s)
9 now=$(date +%s)
10 SevenDaysAgo=$(($now - 36000 * $Days))
11 if [ $SevenDaysAgo -gt $ctimeU ]
12 then
13 $(rm $file)#此处删除文件
14 fi
15 else
16 echo ""
17 fi
18 done
19 }
20 Days=7
21 Path="/var/www/***/log"
22 del7daysAgoLog $Path $Days
23
2 * 删除7天前的日志
3 * @param $logPath
4 */
5 function del7daysAgoLog($logPath) {
6 if(empty($logPath))return;
7 $handle = opendir($logPath);
8 while(($file = readdir($handle)) !== false){
9 $pos = strpos($file, '.log');
10 if ($pos !== false && (strtotime("-1 week") > fileatime($logPath . $file))) {
11 unlink($logPath . $file);
12 }
13 }
14 }
shell 版本
1 #!/bin/sh2 function del7daysAgoLog (){
3 for file in $(ls $1)
4 do
5 if [ "${file##*.}" = "log" ]
6 then
7 ctime=$(stat $1/$file -c "%y")
8 ctimeU=$(date -d "$ctime" +%s)
9 now=$(date +%s)
10 SevenDaysAgo=$(($now - 36000 * $Days))
11 if [ $SevenDaysAgo -gt $ctimeU ]
12 then
13 $(rm $file)#此处删除文件
14 fi
15 else
16 echo ""
17 fi
18 done
19 }
20 Days=7
21 Path="/var/www/***/log"
22 del7daysAgoLog $Path $Days
23
shell 版本比较麻烦 关键我linux转换不熟悉
相关阅读 更多 +