linux下删除7天前日志的代码(php+shell)
作者:bea
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 !==
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转换不熟悉
有用 | 无用
代码如下:
/**
* 删除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转换不熟悉
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 兼容性比较好的PHP生成缩略图的代码
- php的日期处理函数及uchome的function_coomon中日期处理函数的研究
- PHP日期处理函数 整型日期格式
- Base64在线编码解码实现代码 演示与下载
- php !function_exists("T7FC56270E7A70FA81A5935B72EACBE29"))代码解密
- PHP备份/还原MySQL数据库的代码
- php循环检测目录是否存在并创建(循环创建目录)
- 全局记录程序片段的运行时间 正确找到程序逻辑耗时多的断点
- Discuz Uchome ajaxpost小技巧
- php INI配置文件的解析实现分析
- PHP strncasecmp字符串比较的小技巧
- php simplexmlElement操作xml的命名空间实现代码
- array_multisort实现PHP多维数组排序示例讲解
- PHP 设置MySQL连接字符集的方法
- php array_unique之后json_encode需要注意
- 从php核心代码分析require和include的区别
- 深入理解PHP之require/include顺序 推荐
- PHP中foreach循环中使用引用要注意的地方
- PHP开发中四种查询返回结果分析