import osimport timeimport subprocessimport datetimefrom apscheduler.schedulers.blocking import BlockingSchedulerday_log_file = '/root/logdir/info.kongdao.log'req_count_log_path = '/root/logdir/request/req_count/'# 定时任务,每天晚上分析当天的日志数据def log_job():# 执行shell命令获取当日请求数req_count_command = 'grep "发起查询请求" ' + day_log_file + ' | wc -l'req_count_out = subprocess.check_output(req_count_command, shell=True, encoding='UTF-8')req_count = req_count_out.strip()# 当天日期date = str(datetime.date.today())format_date = date.replace('-', '')# 获取文件的创建时间infos = os.stat(day_log_file)ctime = time.strftime('%Y%m%d', time.localtime(infos.st_ctime))# request计数存储的文件名file_name = format_date[:6]# 若info.kongdao.log不是当天的日志,则当天请求数量为0if format_date != ctime:req_count = '0'req_count_log = date + ' ' + req_countrecord_log_command = "echo '" + req_count_log + "' >>" + req_count_log_path + file_namesubprocess.check_output(record_log_command, shell=True, encoding='UTF-8')if __name__ == '__main__':sched = BlockingScheduler()# 每天23:55执行sched.add_job(log_job, 'cron', hour=23, minute=55)sched.start()
100字范文,内容丰富有趣,生活中的好帮手!