Home >Backend Development >PHP Tutorial >shellexecuteex failed. Code to delete logs 7 days ago under linux php+shell
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);
}
}
}
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
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.