广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python 解析 crontab配置
  • 791
分享到

python 解析 crontab配置

pythoncrontab 2023-01-31 05:01:14 791人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

接触python一段时间了,最近要用py做个 监控功能,需要解析crontab中的配置信息, 本想偷懒一下,直接 百度/谷哥出来,无奈半天没找着,只好自己写一个,实现代码及使用 实例如下,望各位路过的大虾大神不吝赐教,能指点得到更优的处理

接触python一段时间了,最近要用py做个 监控功能,需要解析crontab中的配置信息, 本想偷懒一下,直接 百度/谷哥出来,无奈半天没找着,只好自己写一个,实现代码及使用 实例如下,望各位路过的大虾大神不吝赐教,能指点得到更优的处理办法:


#/usr/bin/env Python
#_*_coding:utf-8_*_

# Copyright (c) 2013 stephen <zhangxmGogo@gmail.com>
# All rights reserved

"""
1.解析 crontab 配置文件中的五个数间参数(分 时 日 月 周),获取他们对应的取值范围
2.将时间戳与crontab配置中一行时间参数对比,判断该时间戳是否在配置设定的时间范围内
"""

#$Id $

import re, time, sys

def get_struct_time(time_stamp_int):
____"""
____按整型时间戳获取格式化时间 分 时 日 月 周
____Args:
________time_stamp_int 为传入的值为时间戳(×××),如:1332888820
________经过localtime转换后变成
________time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=0)
____Return:
________list____返回 分 时 日 月 周
____"""

____st_time = time.localtime(time_stamp_int)
____return [st_time.tm_min, st_time.tm_hour, st_time.tm_mday, st_time.tm_mon, st_time.tm_wday]


def get_strptime(time_str, str_fORMat):
____"""从字符串获取 整型时间戳
____Args:
________time_str 字符串类型的时间戳 如 '31/Jul/2013:17:46:01'
________str_format 指定 time_str 的格式 如 '%d/%b/%Y:%H:%M:%S'
____Return:
________返回10位整型(int)时间戳,如 1375146861
____"""
____return int(time.mktime(time.strptime(time_str, str_format)))

def get_str_time(time_stamp, str_format='%Y%m%d%H%M'):
____"""
____获取时间戳,
____Args:
________time_stamp 10位整型(int)时间戳,如 1375146861
________str_format 指定返回格式,值类型为 字符串 str
____Rturn:
________返回格式 默认为 年月日时分,如2013年7月9日1时3分 :201207090103
____"""
____return time.strftime("%s" % str_format, time.localtime(time_stamp))

def match_cont(patten, cont):
____"""
____正则匹配(精确符合的匹配)
____Args:
________patten 正则表达式
________cont____ 匹配内容
____Return:
________True or False
____"""
____res = re.match(patten, cont)
____if res:
________return True
____else:
________return False

def handle_num(val, ranges=(0, 100), res=list()):
____"""处理纯数字"""
____val = int(val)
____if val >= ranges[0] and val <= ranges[1]:
________res.append(val)
____return res

def handle_nlist(val, ranges=(0, 100), res=list()):
____"""处理数字列表 如 1,2,3,6"""
____val_list = val.split(',')
____for tmp_val in val_list:
________tmp_val = int(tmp_val)
________if tmp_val >= ranges[0] and tmp_val <= ranges[1]:
____________res.append(tmp_val)
____return res

def handle_star(val, ranges=(0, 100), res=list()):
____"""处理星号"""
____if val == '*':
________tmp_val = ranges[0]
________while tmp_val <= ranges[1]:
____________res.append(tmp_val)
____________tmp_val = tmp_val + 1
____return res

def handle_starnum(val, ranges=(0, 100), res=list()):
____"""星号/数字 组合 如 */3"""
____tmp = val.split('/')
____val_step = int(tmp[1])
____if val_step < 1:
________return res
____val_tmp = int(tmp[1])
____while val_tmp <= ranges[1]:
________res.append(val_tmp)
________val_tmp = val_tmp + val_step
____return res

def handle_range(val, ranges=(0, 100), res=list()):
____"""处理区间 如 8-20"""
____tmp = val.split('-')
____range1 = int(tmp[0])
____range2 = int(tmp[1])
____tmp_val = range1
____if range1 < 0:
________return res
____while tmp_val <= range2 and tmp_val <= ranges[1]:
________res.append(tmp_val)
________tmp_val = tmp_val + 1
____return res

def handle_rangedv(val, ranges=(0, 100), res=list()):
____"""处理区间/步长 组合 如 8-20/3 """
____tmp = val.split('/')
____range2 = tmp[0].split('-')
____val_start = int(range2[0])
____val_end = int(range2[1])
____val_step = int(tmp[1])
____if (val_step < 1) or (val_start < 0):
________return res
____val_tmp = val_start
____while val_tmp <= val_end and val_tmp <= ranges[1]:
________res.append(val_tmp)
________val_tmp = val_tmp + val_step
____return res

def parse_conf(conf, ranges=(0, 100), res=list()):
____"""解析crontab 五个时间参数中的任意一个"""
____#去除空格,再拆分
____conf = conf.strip(' ').strip(' ')
____conf_list = conf.split(',')
____other_conf = []
____number_conf = []
____for conf_val in conf_list:
________if match_cont(PATTEN['number'], conf_val):
____________#记录拆分后的纯数字参数
____________number_conf.append(conf_val)
________else:
____________#记录拆分后纯数字以外的参数,如通配符 * , 区间 0-8, 及 0-8/3 之类
____________other_conf.append(conf_val)
____if other_conf:
________#处理纯数字外各种参数
________for conf_val in other_conf:
____________for key, ptn in PATTEN.items():
________________if match_cont(ptn, conf_val):
____________________res = PATTEN_HANDLER[key](val=conf_val, ranges=ranges, res=res)
____if number_conf:
________if len(number_conf) > 1 or other_conf:
____________#纯数字多于1,或纯数字与其它参数共存,则数字作为时间列表
____________res = handle_nlist(val=','.join(number_conf), ranges=ranges, res=res)
________else:
____________#只有一个纯数字存在,则数字为时间 间隔
____________res = handle_num(val=number_conf[0], ranges=ranges, res=res)
____return res

def parse_crontab_time(conf_string):
____"""
____解析crontab时间配置参数
____Args:
________conf_string____ 配置内容(共五个值:分 时 日 月 周)
________________________ 取值范围 分钟:0-59 小时:1-23 日期:1-31 月份:1-12 星期:0-6(0表示周日)
____Return:
________crontab_range____list格式,分 时 日 月 周 五个传入参数分别对应的取值范围
____"""
____time_limit____= ((0, 59), (1, 23), (1, 31), (1, 12), (0, 6))
____crontab_range = []
____clist________ = []
____conf_length = 5
____tmp_list____ = conf_string.split(' ')
____for val in tmp_list:
________if len(clist) == conf_length:
____________break
________if val:
____________clist.append(val)
____
____if len(clist) != conf_length:
________return -1, 'config error whith [%s]' % conf_string
____cindex = 0
____for conf in clist:
________res_conf = []
________res_conf = parse_conf(conf, ranges=time_limit[cindex], res=res_conf)
________if not res_conf:
____________return -1, 'config error whith [%s]' % conf_string
________crontab_range.append(res_conf)
________cindex = cindex + 1
____return 0, crontab_range

def time_match_crontab(crontab_time, time_struct):
____"""
____将时间戳与crontab配置中一行时间参数对比,判断该时间戳是否在配置设定的时间范围内
____Args:
________crontab_time____crontab配置中的五个时间(分 时 日 月 周)参数对应时间取值范围
________time_struct____ 某个整型时间戳,如:1375027200 对应的 分 时 日 月 周
____Return:
________tuple 状态码, 状态描述
____"""
____cindex = 0
____for val in time_struct:
________if val not in crontab_time[cindex]:
____________return 0, False
________cindex = cindex + 1
____return 0, True

def close_to_cron(crontab_time, time_struct):
____"""coron的指定范围(crontab_time)中 最接近 指定时间 time_struct 的值"""
____close_time = time_struct
____cindex = 0
____for val_struct in time_struct:
________offset_min = val_struct
________val_close = val_struct
________for val_cron in crontab_time[cindex]:
____________offset_tmp = val_struct - val_cron
____________if offset_tmp > 0 and offset_tmp < offset_min:
________________val_close = val_struct
________________offset_min = offset_tmp
________close_time[cindex] = val_close
________cindex = cindex + 1
____return close_time

def cron_time_list(
________cron_time,
________year_num=int(get_str_time(time.time(), "%Y")),
________limit_start=get_str_time(time.time(), "%Y%m%d%H%M"),
________limit_end=get_str_time(time.time() + 86400, "%Y%m%d%H%M")
____):
____#print "\nfrom ", limit_start , ' to ' ,limit_end
____"""
____获取crontab时间配置参数取值范围内的所有时间点 的 时间戳
____Args:
________cron_time 符合crontab配置指定的所有时间点
________year_num____指定在哪一年内 获取
________limit_start 开始时间
____Rturn:
________List________所有时间点组成的列表(年月日时分 组成的时间,如2013年7月29日18时56分:201307291856)
____"""
____#按小时 和 分钟组装
____hour_minute = []
____for minute in cron_time[0]:
________minute = str(minute)
________if len(minute) < 2:
____________minute = '0%s' % minute
________for hour in cron_time[1]:
____________hour = str(hour)
____________if len(hour) < 2:
________________hour = '0%s' % hour
____________hour_minute.append('%s%s' % (hour, minute))
____#按天 和 小时组装
____day_hm = []
____for day in cron_time[2]:
________day = str(day)
________if len(day) < 2:
____________day = '0%s' % day
________for hour_mnt in hour_minute:
____________day_hm.append('%s%s' % (day, hour_mnt))
____#按月 和 天组装
____month_dhm = []
____#只有30天的月份
____month_short = ['02', '04', '06', '09', '11']
____for month in cron_time[3]:
________month = str(month)
________if len(month) < 2:
____________month = '0%s' % month
________for day_hm_s in day_hm:
____________if month == '02':
________________if (((not year_num % 4 ) and (year_num % 100)) or (not year_num % 400)):
____________________#闰年2月份有29天
____________________if int(day_hm_s[:2]) > 29:
________________________continue
________________else:
____________________#其它2月份有28天
____________________if int(day_hm_s[:2]) > 28:
________________________continue
____________if month in month_short:
________________if int(day_hm_s[:2]) > 30:
____________________continue
____________month_dhm.append('%s%s' % (month, day_hm_s))
____#按年 和 月组装
____len_start = len(limit_start)
____len_end = len(limit_end)
____month_dhm_limit = []
____for month_dhm_s in month_dhm:
________time_ymdhm = '%s%s' % (str(year_num), month_dhm_s)
________#开始时间\结束时间以外的排除
________if (int(time_ymdhm[:len_start]) < int(limit_start)) or \
________ (int(time_ymdhm[:len_end]) > int(limit_end)):
____________continue
________month_dhm_limit.append(time_ymdhm)
____if len(cron_time[4]) < 7:
________#按不在每周指定时间的排除
________month_dhm_week = []
________for time_minute in month_dhm_limit:
____________str_time = time.strptime(time_minute, '%Y%m%d%H%M%S')
____________if str_time.tm_wday in cron_time[4]:
________________month_dhm_week.append(time_minute)
________return month_dhm_week
____return month_dhm_limit


#crontab时间参数各种写法 的 正则匹配
PATTEN = {
____#纯数字
____'number':'^[0-9]+$',
____#数字列表,如 1,2,3,6
____'num_list':'^[0-9]+([,][0-9]+)+$',
____#星号 *
____'star':'^\*$',
____#星号/数字 组合,如 */3
____'star_num':'^\*\/[0-9]+$',
____#区间 如 8-20
____'range':'^[0-9]+[\-][0-9]+$',
____#区间/步长 组合 如 8-20/3
____'range_div':'^[0-9]+[\-][0-9]+[\/][0-9]+$'
____#区间/步长 列表 组合,如 8-20/3,21,22,34
____#'range_div_list':'^([0-9]+[\-][0-9]+[\/][0-9]+)([,][0-9]+)+$'
____}
#各正则对应的处理方法
PATTEN_HANDLER = {
____'number':handle_num,
____'num_list':handle_nlist,
____'star':handle_star,
____'star_num':handle_starnum,
____'range':handle_range,
____'range_div':handle_rangedv
}

def main():
____"""测试用实例"""
____#crontab配置中一行时间参数
____conf_string = '*/10 * * * * (cd /opt/pythonpm/devpapps;' \
________________ ' /usr/local/bin/python2.5 data_test.py>>output_error.txt)'
____#时间戳
____time_stamp = int(time.time())


____#解析crontab时间配置参数 分 时 日 月 周 各个取值范围
____res, desc = parse_crontab_time(conf_string)
____if res == 0:
________cron_time = desc
____else:
________print desc
________sys, exit(-1)
____print "\nconfig:", conf_string
____print "\nparse result(range for crontab):"
____
____print " minute:", cron_time[0]
____print " hour: ", cron_time[1]
____print " day: ", cron_time[2]
____print " month: ", cron_time[3]
____print " week day:", cron_time[4]

____#解析 时间戳对应的 分 时 日 月 周
____time_struct = get_struct_time(time_stamp)
____print "\nstruct time(minute hour day month week) for %d :" % \
________ time_stamp, time_struct

____#将时间戳与crontab配置中一行时间参数对比,判断该时间戳是否在配置设定的时间范围内
____match_res = time_match_crontab(cron_time, time_struct)
____print "\nmatching result:", match_res

____#crontab配置设定范围中最近接近时指定间戳的一组时间
____most_close = close_to_cron(cron_time, time_struct)
____print "\nin range of crontab time which is most colse to struct ", most_close

____time_list = cron_time_list(cron_time)
____print "\n\n %d times need to tart-up:\n" % len(time_list)
____print time_list[:10], '...'


if __name__ == '__main__':
____#请看 使用实例
____
____main()


--结束END--

本文标题: python 解析 crontab配置

本文链接: https://www.lsjlt.com/news/188594.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python 解析 crontab配置
    接触python一段时间了,最近要用py做个 监控功能,需要解析crontab中的配置信息, 本想偷懒一下,直接 百度/谷哥出来,无奈半天没找着,只好自己写一个,实现代码及使用 实例如下,望各位路过的大虾大神不吝赐教,能指点得到更优的处理...
    99+
    2023-01-31
    python crontab
  • Python中怎么解析配置文件
    这篇文章将为大家详细讲解有关Python中怎么解析配置文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在...
    99+
    2023-06-17
  • 详解使用python crontab设置linux定时任务
    熟悉linux的朋友应该知道在linux中可以使用crontab设置定时任务。可以通过命令crontab -e编写任务。当然也可以直接写配置文件设置任务。 但是有时候希望通过脚本自动设置,比如我们应用程序部...
    99+
    2022-06-04
    详解 python linux
  • 如何使用Linux下Crontab配置文件
    本篇内容介绍了“如何使用Linux下Crontab配置文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!【全局(系统)配置文件】代码如下:/...
    99+
    2023-06-13
  • 使用 Python 解析配置文件格式
    第一步是选择配置文件的格式:INI、JSON、YAML 或 TOML。 有时,程序需要足够的参数,将它们全部作为命令行参数或环境变量既不让人愉快也不可行。 在这些情况下,你将需要使用配置文件。 有几种流行的配置文件格...
    99+
    2022-06-02
    Python 解析配置文件 Python 配置文件
  • 怎么用Python解析toml配置文件
    举个例子有了 ini 和 yaml,相信 toml 学习来也很简单,先直接看一个例子吧。import toml config = """ title = "toml 小栗子" [owne...
    99+
    2023-05-21
    Python toml
  • 配置 nginx 解析 php
    修改 nginx 配置文件 vim /etc/nginx/nginx.conf 在 server 中插入如下代码: location ~ \.php$ {     try_files $uri =404;     fastcgi_pass ...
    99+
    2023-10-23
    nginx php 运维
  • Ubuntu22配置dns解析
    目录 一、现象:无法解析域名  二、方式一:配置resolv.conf 三、方式二:使用netplan管理网络 一、现象:无法解析域名   查看一下dns配置 cat etc/resolv.conf   二、方式一:配置resolv....
    99+
    2023-09-06
    服务器 运维
  • Mysql 文件配置解析
    目录client 端配置mysqld 端配置其他设置sql_mode总结前言: mysql数据库在日常工作开发中经常用到的存储设备, 之前已经分享了面试过程中经常被问到的mysql优...
    99+
    2022-11-13
  • Redis配置文件解析
    Redis概述:    是一个基于Key-Value的持久化数据库存储,支持丰富的数据类型,用C语言编写,可基于内存又可持久化的日志型、Key-Value数据库,并提...
    99+
    2022-10-18
  • Python解析toml配置文件的方法分享
    目录楔子举个例子注释键值对字符串整数浮点数布尔值日期数组表行内表表数组楔子 上一篇文章我们介绍了 yaml,虽然 yaml 的表达能力已经很丰富了,但 GitHub 觉得还是不够优雅...
    99+
    2022-11-11
  • Python 中怎么利用ConfigParser解析配置模块
    这篇文章将为大家详细讲解有关Python 中怎么利用ConfigParser解析配置模块,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。基本的读取配置文件-read(filename) 直接读...
    99+
    2023-06-04
  • Python实现解析yaml配置文件的示例详解
    目录楔子字典数组标量引用生成 yaml 文件楔子 前面我们介绍了 ini 格式的配置文件,本次来看看 yaml,它的表达能力相比 ini 更加的强大。yaml 文件以 .yml 结尾...
    99+
    2022-11-11
  • Python实现解析ini配置文件的示例详解
    目录楔子ini 文件特殊格式小结楔子 在开发过程中,配置文件是少不了的,只不过我们有时会将 py 文件作为配置文件(config.py),然后在其它的模块中直接导入。这样做是一个好主...
    99+
    2022-11-11
  • 如何使用Linux中Crontab基本组成与配置
    这篇文章主要介绍“如何使用Linux中Crontab基本组成与配置”,在日常操作中,相信很多人在如何使用Linux中Crontab基本组成与配置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何使用Linux...
    99+
    2023-06-13
  • Centos下通过配置crontab来定时执行任务
    本篇内容主要讲解“Centos下通过配置crontab来定时执行任务”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Centos下通过配置crontab来定时执行任务”吧!Centos下可以通过配...
    99+
    2023-06-10
  • 配置Python解释器
    在运行python项目时,如果代码目录中没有venv目录(即别人已经配置好的解释器),往往不能直接运行,因此需要自己添加项目对应的解释器,即使有别人配好的解释器,也会因为安装的python位置差异导致解释器不可用,本文将对这两种情况分别解决...
    99+
    2023-10-07
    python pycharm 开发语言 ide
  • springBoot中的properties配置解析
    目录1、配置文件的格式2、配置文件的加载1-公共配置文件:application.properties2-自定义配置文件:donghao.properties3、配置项的使用3-绑定...
    99+
    2022-11-13
  • SAP 打印配置(SPAD)解析
    一、SAP打印原理  SAP的打印过程分两个步骤: 创建假脱机请求 创建输出请求 在点击打印按钮后,系统会提示创建假脱机请求后,你可以选择直接生成输出请求,或者手动生成输出请求,产生输出请求后,系统会根据输出设备的配置,调用相关的打印程序;...
    99+
    2023-09-25
    servlet 开发语言 前端 服务器
  • channel如何解析和配置
    这篇文章主要为大家展示了“channel如何解析和配置”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“channel如何解析和配置”这篇文章吧。 RMAN ch...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作