Home >Backend Development >PHP Tutorial >shellexecuteex failed. Code to delete logs 7 days ago under linux php+shell

shellexecuteex failed. Code to delete logs 7 days ago under linux php+shell

WBOY
WBOYOriginal
2016-07-29 08:44:051373browse

PHP version:

Copy code The code is as follows:


/**
* Delete logs from 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

Copy code code 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 file here
fi
else
echo ""
fi
done
}
Days=7
Path ="/var/www/***/log"
del7daysAgoLog $Path $Days


shell The version is more troublesome. The key is that I am not familiar with linux conversion.

The above introduces the php+shell code for deleting logs 7 days ago under Linux when shellexecuteex fails, including the content of shellexecuteex failure. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn