linux下刪除7天前日志的代碼(php+shell)
更新時間:2011年01月02日 00:44:22 作者:
shell 版本比較麻煩 關鍵我linux轉(zhuǎn)換不熟悉
PHP版本:
/**
* 刪除7天前的日志
* @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 版本
#!/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)#此處刪除文件
fi
else
echo ""
fi
done
}
Days=7
Path="/var/www/***/log"
del7daysAgoLog $Path $Days
shell 版本比較麻煩 關鍵我linux轉(zhuǎn)換不熟悉
復制代碼 代碼如下:
/**
* 刪除7天前的日志
* @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 版本
復制代碼 代碼如下:
#!/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)#此處刪除文件
fi
else
echo ""
fi
done
}
Days=7
Path="/var/www/***/log"
del7daysAgoLog $Path $Days
shell 版本比較麻煩 關鍵我linux轉(zhuǎn)換不熟悉
相關文章
php 調(diào)試利器debug_print_backtrace()
debug_print_backtrace() 是一個很低調(diào)的函數(shù),很少有人注意過它. 不過當我對著一個對象調(diào)用另一個對象再調(diào)用其它的對象和文件中的一個函數(shù)出錯時,它正在一邊笑呢2012-07-07字符串長度函數(shù)strlen和mb_strlen的區(qū)別示例介紹
strlen和mb_strlen的區(qū)別,但是對于一些初學者來說,如果不看手冊,也許不太清楚其中的區(qū)別,下面與大家分享下兩者之間的區(qū)別2014-09-09php中Array2xml類實現(xiàn)數(shù)組轉(zhuǎn)化成XML實例
這篇文章主要介紹了php中Array2xml類實現(xiàn)數(shù)組轉(zhuǎn)化成XML的方法,實例分析了數(shù)組轉(zhuǎn)化成XML實現(xiàn)類Array2xml,是非常實用的數(shù)組轉(zhuǎn)化技巧,需要的朋友可以參考下2014-12-12