Code for deleting logs 7 days ago in linux (php + shell) _ PHP Tutorial

Source: Internet
Author: User
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 );...

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.