Code for deleting logs 7 days ago in linux (php + shell)
Source: Internet
Author: User
The shell version is troublesome. I am not familiar with the PHP version for linux conversion:
The code is as follows:
/**
* Delete logs 7 days ago
* @ 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 version
The code is as follows:
#! /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) # Delete an object here
Fi
Else
Echo ""
Fi
Done
}
Days = 7
Path = "/var/www/***/log"
Del7daysAgoLog $ Path $ Days
The shell version is troublesome. I am not familiar with linux conversion.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.