定时清理日志文件-python实现
1、编写脚本#! /usr/bin/python# -*- coding: utf-8 -*import osimport os.path, time, datetimelogdir = "/data0/www/applogs"for parent, dirnames, filenames in os.walk(logdir): for f
·
1、脚本代码如下
#! /usr/bin/python
# -*- coding: utf-8 -*
import os
import os.path, time, datetime
logdir = "/data0/www/applogs"
for parent, dirnames, filenames in os.walk(logdir):
for filename in filenames:
fullname = parent + "/" + filename #文件全称
createTime = int(os.path.getctime(fullname)) #文件创建时间
nDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 2)) #当前时间的n天前的时间
timeStamp = int(time.mktime(nDayAgo.timetuple()))
if createTime < timeStamp: #创建时间在n天前的文件删除
os.remove(os.path.join(parent,filename))
0 0 * * * /root/clean_log.py >> /tmp/clean_log.log
更多推荐
已为社区贡献3条内容
所有评论(0)