100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Python编写定时执行脚本

Python编写定时执行脚本

时间:2023-11-27 15:06:12

相关推荐

Python编写定时执行脚本

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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。