linux下刪除7天前日志的代碼(php+shell)
更新時(shí)間:2011年01月02日 00:44:22 作者:
shell 版本比較麻煩 關(guān)鍵我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 版本比較麻煩 關(guān)鍵我linux轉(zhuǎn)換不熟悉
復(fù)制代碼 代碼如下:
/**
* 刪除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 版本
復(fù)制代碼 代碼如下:
#!/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 版本比較麻煩 關(guān)鍵我linux轉(zhuǎn)換不熟悉
您可能感興趣的文章:
相關(guān)文章
php 調(diào)試?yán)鱠ebug_print_backtrace()
debug_print_backtrace() 是一個(gè)很低調(diào)的函數(shù),很少有人注意過(guò)它. 不過(guò)當(dāng)我對(duì)著一個(gè)對(duì)象調(diào)用另一個(gè)對(duì)象再調(diào)用其它的對(duì)象和文件中的一個(gè)函數(shù)出錯(cuò)時(shí),它正在一邊笑呢2012-07-07字符串長(zhǎng)度函數(shù)strlen和mb_strlen的區(qū)別示例介紹
strlen和mb_strlen的區(qū)別,但是對(duì)于一些初學(xué)者來(lái)說(shuō),如果不看手冊(cè),也許不太清楚其中的區(qū)別,下面與大家分享下兩者之間的區(qū)別2014-09-09php使用GD庫(kù)創(chuàng)建圖片縮略圖的方法
這篇文章主要介紹了php使用GD庫(kù)創(chuàng)建圖片縮略圖的方法,涉及php使用GD庫(kù)操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06PHP簡(jiǎn)單實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載的方法,涉及php針對(duì)文件傳輸?shù)南嚓P(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09php中Array2xml類實(shí)現(xiàn)數(shù)組轉(zhuǎn)化成XML實(shí)例
這篇文章主要介紹了php中Array2xml類實(shí)現(xiàn)數(shù)組轉(zhuǎn)化成XML的方法,實(shí)例分析了數(shù)組轉(zhuǎn)化成XML實(shí)現(xiàn)類Array2xml,是非常實(shí)用的數(shù)組轉(zhuǎn)化技巧,需要的朋友可以參考下2014-12-12