In linux, the code for deleting logs 7 days ago (php + shell ). PHP version: Copy the code as follows: *** delete logs 7 days ago * @ param $ logPath * functiondel7daysAgoLog ($ logPath) {if (empty ($ logPath) return; $ handleopendir ($ logPath); PHP version:
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 authorization code is as follows:/*** delete logs generated seven days ago * @ param $ logPath */function del7daysAgoLog ($ logPath) {if (empty ($ logPath) return; $ handle = opendir ($ logPath );...